Scoping problems with RPC

Options
Hi! 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 think 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.



Answers

  • jeanfabre
    Options
    Hi,

    It's because 'myPhotonView' is null, so before, make a check that myPhotonView is not null, if it is, log an error, else you can proceed.

    so you'll need to look where you actually set 'myPhotonView' and debug that part.

    Bye,

    Jean
  • ekarnis
    Options
    Hi,

    Thanks for answering, but myPhotonView is being set correctly. This is proved by the RPC working perfectly outside the if statement.

    My problem is that myPhotonView is filled outside the if statement and null inside of it.

    If anyone else is having the same problem, I got around it by instantiating gameobjects named after values that I wanted to transmit.
  • jeanfabre
    Options
    Hi,

    The question would be then what is this flag representing, and how does it matches the code where you actually get myPhotonView. So yes, possible you have a race condition in your code that makes it so, but it's nothing we can comment accuratly without seeing the whole code and where you get the myphotonView.

    bye,

    Jean