Am I supposed to keep checking PhotonView objects != null
Options
jandulio
✭
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.
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!
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!
0
Best Answer
-
Generally: yes! But if it gets set to null from nowhere, then of course not. Same goes for objects with photon.isMine==true5
Answers
-
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!0 -
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.0 -
Generally: yes! But if it gets set to null from nowhere, then of course not. Same goes for objects with photon.isMine==true5
-
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!0