应该填充对象时为空,唯一的原因似乎是任意的

时间:2016-07-14 20:52:09

标签: c# unity3d scoping photon

我对PUN和Unity有些陌生,所以希望我犯了一些初学者的错误。

void Update(){

if(launch == true){

Debug.Log("launched");

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

}

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

}

当我运行上面的代码时,我可以看到在控制台中启动但是我得到了

  

“NullReferenceException:对象引用未设置为的实例   对象“在这一行:myPhotonView.RPC(”ChangeScene“,   PhotonTargets.All);

当我用该行注释掉并且在if语句之外的行取消注释时运行它时,没有空引用异常并且该方法执行。上面的代码中是否有错误,或者这是Photon中的错误?

任何帮助都非常感谢。如果有人认为有必要,我可以发布完整的代码或错误消息。

此外,这不太重要,但myPhotonView在我创建的任何方法中都为null,这些方法不是常规的MonoBehaviour方法,如Start或Update,即使我将其设置为全局变量并在尝试访问它之前将其填入Start 。这似乎可能与我遇到的其他问题有关,这是我为此使用Update的唯一原因。

编辑:这是整个文件:

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);
    }
}

编辑:我从未设法解决这个问题,但我确实解决了这个问题。我创建了游戏对象,为游戏中的其他玩家提供价值。请告诉我更多信息

0 个答案:

没有答案