Assign Player Index (GetRoomIndex) Utility Script

I am having a dilemma where I don't know how to assign each player there own index number. To call the PlayerRoomIndexing script I am successfully using the namespace 'using Exitgames.UtilityScipts' which is working fine and in my update function for my player's script I am calling ' playerIndex = PlayerRoomIndexing.instance.GetRoomIndex(photonView.owner);' I don't know if this is right but this was the closest thing I found online. I want to be able to give each player their own index number and then reference that number in another script to be able to link it to a scoreboard. I would be grateful for some help.


  • Bump
  • Why not just use the ActorId?

  • I have an open party system, all the players are in a party and can go into minigames and after the game everyone goes back to the lobby. If players leave and others join the can cause the actorID to overlap the max players (8) so someone could have an ID of 16 for example, and my minigames need numbers from 1-8 for them to work. I need a system that gives players their own ID number from 1- the players in the game, so I thought player indexing would be the best way forward.
  • Bump
  • There is nothing built into Pun2 for this, you would probably task your Master Client to assign values when new players join, and RPC all players that index value assignment.
  • hey @emotitron,

    actually there is a UtilityScript and extension methods for PUN Classic and for PUN 2.
    - for PUN Classic: PlayerRoomIndexing component, add it to the scene, use photonPlayer.GetRoomIndex() extension method. namespace: ExitGames.UtilityScripts
    - for PUN 2: PlayerNumbering component, add it to the scene. use player.GetPlayerNumber() extension method. namespace: Photon.Pun.UtilityScripts.


    sorry for the delay on this one but sometimes you need to take a look at the code and try to understand it.

    it's more convenient to use extension method directly from PhotonPlayer class as explained above.
    this extension method itself uses PlayerRoomIndexing.instance.GetRoomIndex internally.
  • @JohnTube I always forget about the Utilities, since I only work on the core. Good catch.
  • Thank you both for your replies. Sorry but what does the extension method actually mean? And how do I use it? Thank you again
  • You can learn about extension methods here.

    In our case, you need to use photonPlayer.GetRoomIndex() which is the extension method for the PhotonPlayer class instead of calling PlayerRoomIndexing.instance.GetRoomIndex.
  • Ahh I see let me try that out, give the article a read and get back to you on how it worked out. Thank you again!
  • When I call photonPlayer.GetRoomIndex() in the Start function of my player script it returns the error ‘Object reference not set to an instance of an object’ Exitgames.UtilityScripts.PlayerRoomIndexing (PhotonPlayer player) (at Assets/Photon Unity Networking/UtilityScripts/PhotonPlayer/PlayerRoomIndexing.cs: 157)
  • Line 157 is:
    if(_indexLUT!=null && _indexLUT.ContainsKey(player.ID))