Issue with the PlayerNumbering.cs script in the PUN Utility scripts
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Issue with the PlayerNumbering.cs script in the PUN Utility scripts
ClusterFame
2021-06-22 13:24:47
Hello! I've been using Photon PUN for my game, and I'm encountering an issue. I'm using the PlayerNumbering script to identify the number of the local player and spawn them at different spawn points, and for some reason, the player number gets incremented every time a new scene is loaded,
Here is what I've written at the top of the script:
using Photon.Pun.UtilityScripts;
Here is my code in the Start function:
int playerNumber = PhotonNetwork.LocalPlayer.GetPlayerNumber();
Debug.Log(playerNumber);
The above code returns "-1" when the player is in scene 1, and it returns "0" when the player transitions to scene 2.
The weird part is that when there are two players in the scene, and the master client brings the other player to scene 2, their player number (that I get from PhotonNetwork.LocalPlayer.GetPlayerNumber();) becomes 0. It becomes 0 for both players, and because of that, they spawn at the same time.
Comments
ClusterFame
2021-06-22 13:40:12
This is what the PlayerNumbering.cs script looks like when the Player is in the first scene and "PhotonNetwork.LocalPlayer.GetPlayerNumber();" returns -1.
ClusterFame
2021-06-22 13:43:18
Also, in the first scene, when I again check the output of "PhotonNetwork.LocalPlayer.GetPlayerNumber();", it returns 0, but returns -1 at start.
Did you try the DontDestroyOnLoad option?
It's there so you opt-in to not destroying the object it's on (which has an effect on other components as well).
It returns -1 until you are in a room, iirc.
ClusterFame
2021-06-23 11:22:10
Yes, I did try the DontDestroyOnLoad option. It gives many errors, and doesn't fix any thing. One work around that I found was to not call [PhotonNetwork.LocalPlayer.GetPlayerNumber(); in the start function, because when I call it in the start function, it returns -1, and nothing is consistent, but when I call it after Start function in a coroutine, it returns 0 for player one, 1 for player 2 and works in all scenes without the DoNotDestroyOnLoad option.
It's kind of a stupid work around, but it works.
I think there is a callback which lets you know when the numbers are assigned. It's sadly not simple enough to guarantee it is sorted in Start().
Back to top