The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

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).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

whats casuses this "NullReferenceException: Object reference not set to an instance of an object"

Kevin256
2022-07-06 18:46:20

using Photon.Pun;

using Photon.Realtime;

using UnityEngine;

using UnityEngine.UI;

using ExitGames.Client.Photon;

public class PlayerItems : MonoBehaviourPunCallbacks

{

[SerializeField] Text playerNameInput;

[SerializeField] GameObject leftButton;

[SerializeField] GameObject rightButton;

// Characeter Selection

Hashtable playerProperties = new Hashtable();

[SerializeField] Image playerAvatars;

[SerializeField] Sprite[] avatars;

Player player;

public void setPlayerInfo(Player playerName)

{

playerNameInput.text = playerName.NickName;

player = playerName;

updatePlayerItem(player);

}

public void ApplyLocalChanges()

{

leftButton.SetActive(true);

rightButton.SetActive(true);

}

public void onClickLeftArrow()

{

// I keep recieving "NullReferenceException: Object reference not set to an instance of an object" error

// on line 38 and 53. Does anyone know how to fix this

if ((int)playerProperties["playerAvatars"] == 0)

{

playerProperties["playerAvatars"] = avatars.Length - 1;

}

else

{

playerProperties["playerAvatars"] = (int)playerProperties["playerAvatars"] - 1;

}

PhotonNetwork.SetPlayerCustomProperties(playerProperties);

}

public void onClickRightArrow()

{

// I keep recieving "NullReferenceException: Object reference not set to an instance of an object" error

// on line 38 and 53.

if ((int)playerProperties["playerAvatars"] == avatars.Length - 1)

{

playerProperties["playerAvatars"] = 0;

}

else

{

playerProperties["playerAvatars"] = (int)playerProperties["playerAvatars"] + 1;

}

PhotonNetwork.SetPlayerCustomProperties(playerProperties);

}

public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)

{

if (player == targetPlayer)

{

updatePlayerItem(targetPlayer);

}

}

public void updatePlayerItem(Player player)

{

if (player.CustomProperties.ContainsKey("playerAvatars"))

{

playerAvatars.sprite = avatars[(int)player.CustomProperties["playerAvatars"]];

playerProperties["playerAvatar"] = (int)player.CustomProperties["playerAvatars"];

}

else

{

playerProperties["playerAvatar"] = 0;

}

}

}

Comments

Alejandrazo
2022-08-01 03:58:53

"Object reference not set to an instance of an object".

This error can appears because that GameObject in the scene doesnt exist at this moment maybe it loads after.

Back to top