Why can't I get a list of all the player's GameObjects?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Why can't I get a list of all the player's GameObjects?

jijigri
2021-10-06 20:30:19

Okay, it's driving me a bit crazy, I've been struggling with this for a few days and I can't see why it isn't working.

I have a monster, instantiated over the Network by the MasterClient (so there is only one).

This monster should hold a list of GameObjects, being the players in the scene, and then select a random player to attack each time it wants to attack.

I'm filling the list using GameObject.FindGameObjectsWithTag("Player") and I'm making sure the list is the same size as PhotonNetwork.CurrentRoom.PlayerCount in the Update loop because sometimes the monster's Start() method runs before all the players are completely instantiated, so I'm avoiding that.

Now my problem is that all the players in the List are just the same, the MasterClient, and it doesn't make any sense to me.

The size is the correct size (the number of players in the room), but when I try to Debug.Log() the PhotonView ID of each player, it returns the same ViewID.

By extension, it's only attacking the MasterClient and no other player.

What is happening? And does anyone know the proper way to get all the players? Thank you.

Comments

Klover
2021-12-24 20:39:31

So this would just be a script attached to the Manager gameObject in the scene. Whenever a player loads up the scene, they will instantiate a player object for themselves.

Here on the actual player's object itself, we add it to the list. Ummm I made a bit of an accident there when I put script.playerList.Add(GameObject);

(GameObject) is actually supposed to be (gameObject) yes it has to be lowercase

Back to top