1

I'm somewhat new to PUN and Unity so hopefully, I am making some kind of beginner mistake.

void Update(){

if(launch == true){

Debug.Log("launched");

myPhotonView.RPC("ChangeScene", PhotonTargets.All);

}

//myPhotonView.RPC("ChangeScene", PhotonTargets.All);

}

When I run the code above, I can see launched in the console but I get

"NullReferenceException: Object reference not set to an instance of an object" on this line: myPhotonView.RPC("ChangeScene", PhotonTargets.All);

When I run it with that line commented out and the line that's outside the if statement uncommented, there is no null reference exception and the method executes. Is there a mistake in the above code, or is this a bug in Photon?

Any help at all is much appreciated. I can post the full code or error messages if anyone thinks it's necessary.

Also, this is less important, but myPhotonView is null in any methods I create that aren't regular MonoBehaviour methods like Start or Update even though I set it as a global variable and filled it in Start before I try to access it. This seems like it might be tied to the other problems I'm having, and it's the only reason I'm using Update for this.

Edit: Here's the entire file:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class NetworkedPlayer : Photon.MonoBehaviour{
    public GameObject avatar;
    public PhotonView myPhotonView;
    public bool launch = false;
    public string destination = "";
    //Responsible for avatar movements on the plane
    public Transform playerGlobal;
    //Responsible for headmovements
    public Transform playerLocal;
    public Transform playerRotation;

    void Start (){
        //launch is successfuly set to false
        //if(!launch) Debug.LogError("banana");

        Debug.Log("Instantiated");
        //Necessary for photon voive to work
        var temp1 = PhotonVoiceNetwork.Client; 
        myPhotonView = this.photonView;
        //this ensures that only you can control your player
        if (photonView.isMine){
            Debug.Log("Controlling my avatar");

            playerGlobal = GameObject.Find("OVRPlayerController").transform;
            playerLocal = playerGlobal.Find("OVRCameraRig/TrackingSpace/CenterEyeAnchor/Pivot");
            playerRotation = playerGlobal.Find("OVRCameraRig/TrackingSpace/CenterEyeAnchor");


            this.transform.SetParent(playerLocal);
            this.transform.localPosition = Vector3.zero;
            this.transform.rotation = playerRotation.transform.rotation;

            // avatar.SetActive(false);
            //Throws null
            //GetComponent<PhotonVoiceRecorder> ().enabled = true;
        }
    }               

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
        //This sends your data to the other players in the same room
        if (stream.isWriting){
            stream.SendNext(playerGlobal.position);
            stream.SendNext(playerGlobal.rotation);
            stream.SendNext(playerLocal.localPosition);
            stream.SendNext(playerLocal.localRotation);
        }
        //This recieves information from other players in the same room
        else{
            this.transform.position = (Vector3)stream.ReceiveNext();
            this.transform.rotation = (Quaternion)stream.ReceiveNext();
            avatar.transform.localPosition = (Vector3)stream.ReceiveNext();
            avatar.transform.localRotation = (Quaternion)stream.ReceiveNext();
        }
    }

    public void ChangeAndSyncScene(string dest){
        Debug.LogError("Dest selected: " + dest);
        launch = true;
        destination = dest;
        Update();
    }

    void Update(){
        if(launch == true){
            Debug.LogError("launched");
            myPhotonView.RPC("ChangeScene", PhotonTargets.All);
            ChangeScene();
        }
        myPhotonView.RPC("ChangeScene", PhotonTargets.All);
    }

    [PunRPC]
    void ChangeScene(){
        Debug.LogError("scene changed");
        Application.LoadLevel (destination);
    }
}

Edit: I never managed to fix this, but I did get around it. I created gameobjects to give values to other players in the game. PM me for more info

  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tim Jul 14 '16 at 21:11
  • Show the code where you actually assign `myPhotonView`, and the place where you declare it. – CodeCaster Jul 14 '16 at 21:12
  • 1
    purely FYI, consider just using ordinary Unity built-in networking. there is very little reason to bother with Photon these days. – Fattie Jul 14 '16 at 22:12
  • I uploaded the file above. myPhotonView is declared at the top, and it's assigned in Start(). To Tim, I don't think this is a duplicate at all. I know what a nullref error is, and my question is purely about why it's null in one place and not null in another place when the variable is globally scoped. Joe, I'm sadly already pretty invested in Photon but I might switch if I keep having trouble. – Eric Karnis Jul 14 '16 at 22:30

0 Answers0