Question about FindGameObjectsWithTag function in PUN

I am working on a simple multiplayer card game and I am using FindGameObjectsWithTag to find all the player's gameObjects in the network game. I then assign them to public array Players[]. This is done for all clients, so each client has a list of the player objects.

Sometimes it works when I do a Players[ID].getComponent, but sometimes I get a null exception.

This makes me wonder if the Network Player Objects the array points to are being removed and replaced with a new object, thus causing me to have a null exception, would this be the case?

It would seem so, if it is then each time another script needs to access the array of Player's Object (not the PUN created player but the object they control) I would need to call FindGameObjectsWithTag each time I need to access the array, or would there be a better way to do this?

I was thinking by assigning the found objects to an public array it would be faster to access it.

Comments

  • Better way to maintain player's objects list is handling OnPhotonInstantiate(PhotonMessageInfo info) message. In this case you are sure that only Photon instantiated objects are in list.