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

Options

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.

Answers

  • Klover
    Options

    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