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