Am I supposed to keep checking PhotonView objects != null

Options
When an object is instantiated by Photon and "photonView.isMine" is false, I keep a reference to it and use it to keep track of them in-game.
MainGame mainGameObject;

Awake()
{
  if(photonView.isMine)
  {
     mainGameObject.opponent = this;
  }
}

Update()
{
  if(mainGameObject.opponent != null)  // is this correct?
  {
      // point camera at opponent, get vector to opponent, etc.
  }
}

I think this is a simple question. Am I supposed to check for any references of objects that have a PhotonView attached for != null before acting on them? I have them sprinkled all throughout the code, so I just want to make sure I'm doing things the "Photon" way.

(Sorry about the formatting, code tags didn't seem to work well)

Thanks!

Best Answer

Answers

  • Jasper_w
    Options
    It is kind of confusing to me. So in the beginning you set the opponent to this GameObject of the script.

    So then in the update you check if the opponent is not null.

    I presume the class this is from represents the opponent. So why does the opponent even need to know what mainGameObject.opponent is? It is the opponent and does not need to know this!
  • jandulio
    Options
    Thanks for the reply @Jasper_w !

    My example was a typo. My actual code has a lot more to it so I didn't want to include all of it, and I missed some stuff (obviously) when I recreated it.

    So the question basically is, once I get a reference to a Photon instantiated object that is !photon.isMine, should I keep checking it for !=null when acting on it? NOTE: If !photon.isMine, it means it is an opponent in my example.

    // get reference to an opponent
    if(!photonView.isMine)
    {
    // view is not mine, it is an opponent - get reference
    opponent = this.gameObject;
    }

    // make sure opponent didn't drop before I decide to aim my laser at it, etc.
    if(opponent != null)
    {
    // do various things based on opponent, aim laser at it, put camera between me and them, etc.
    }

    I wonder if I should repost the question with the correct code.
  • Jasper_w
    Options
    Also, if you come across Received "OnSerialization for view ID xxxx. We have no such PhotonView..." on mid game join pls hit me up if you have any info... trying to fix this for days!
  • jandulio
    Options
    @Jasper_w I had that every single time someone joined when using PUN2 v2.12, but it is no longer happening now that I updated to v2.13.
  • Jasper_w
    Options
    @jandulio OMG I will try that, thank you so much!!!!!!!!!!!!!
  • Jasper_w
    Options
    jandulio said:

    @Jasper_w I had that every single time someone joined when using PUN2 v2.12, but it is no longer happening now that I updated to v2.13.

    Yes you saved my sweet ass, thank you sooooo much!!!