NullReferenceException error (68852)

I am trying to use photonView.isMine, but it is not working. I am getting this error.NullReferenceException: Object reference not set to an instance of an object MultiplayerHandler.OnJoinedRoom () (at Assets/Scripts/MultiplayerHandler.cs:20)

Here is my code I am trying to use:

void OnJoinedRoom()
    {
       GameObject player = PhotonNetwork.Instantiate("ClientPlayer", Vector3.zero, Quaternion.identity, 0);
       if (photonView.isMine)
       {
         (player.GetComponent("CharacterMotor") as MonoBehaviour).enabled = true;
         (player.GetComponent("FPSInputController") as MonoBehaviour).enabled = true;
         (player.GetComponent("MouseLook")as MonoBehaviour).enabled = true;
       }

by the way the it says the error was on this line: if (photonView.isMine)

That means the photonVIew is null and not assigned.

Excuse my utter noobiness, but how would I make it assigned?

You should have this somewhere in your code, and this should be called before photonView is used: photonView = ...; Or you can have this, then you assign the photonView in the inspector (only works if the photonView is a component): public TheTypePhotonViewIs photonView;

but photonView is a class the comes with Photon. I think isMine is coming back null but I don't know why.

2 Answers

2

It may be that photonView.isMine expects to be part of a script attached to a GameObject.
I am curious if this script will work if you attach it to the player prefab. Have one script instantiate the player and the other handle enabling your other scripts.

I just had this problem for anybody finding this question in the future in a google search the Error is solved after you add a Photonview script to the same object this script is on.