Every time I click play in Unity, I get this error.
NullReferenceException: Object reference not set to an instance of an object
LobbyManager.PlayerCountUpdate () (at Assets/Scripts/Lobby/LobbyManager.cs:213)
LobbyManager.Update () (at Assets/Scripts/Lobby/LobbyManager.cs:209)
I have tried the following to ensure the ERROR does not pop up:
- I have ensured the inspector with public variables has been allocated with the assigned GameObject.
- I tried changing
roomSize = PhotonNetwork.CurrentRoom.MaxPlayers;
to the start function since its not a variable which require updating but the error still pops up!
Here is My code:
public int playerCount;
int roomSize;
private void Update()
{
//minimum of 2 players in the room && the player who is clicking the start button is the masterclient
if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount >= 2)
{
playButton.SetActive(true);
}
else
{
playButton.SetActive(false);
}
PlayerCountUpdate();
}
public void PlayerCountUpdate()
{
roomSize = PhotonNetwork.CurrentRoom.MaxPlayers;
playerCount = PhotonNetwork.PlayerList.Length;
roomCountDisplay.text = playerCount + " / " + roomSize;
}