[Problem] Reference to player gameObject needed

So I’ve tried using onClientConnect in order to have this:

  public override void OnClientConnect(NetworkConnection conn) {
         ClientScene.Ready(conn);
         Object[] allObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject));
         foreach (GameObject go in allObjects) {
             if (go.tag != "NetworkPlayer") {
                 EnemyController.AddPlayer(go);
             }
         } 
         Debug.Log("test");
         //ClientScene.AddPlayer(0);
     
     }

I have took part of this code from a guy that helped me.
I’m not sure what the ClientScene.AddPlayer(0) server here thus I set it as a comment.
“p” is a list of gameObjects of players and the method AddPlayer() in EnemyController adds to
the list another game object. the Debug log hasn’t worked and I’m trying to get the list
of the players unsuccessfully please help me :open_mouth:

Why not just have the Player objects add themselves when they start?

void Start()
{
    if (isServer)
    {
        EnemyController.AddPlayer(gameObject);
    }
}

Hey, first of all where do I put this code? I need every player to get added . Secondly is gameObject refered to the client’s gameObject?

You’d put that code in a NetworkBehavior attached to the Player gameobject prefab. In any MonoBehaviour or NetworkBehaviour “gameObject” by itself always refers to the GameObject the script is attached to.

You’d need to get a reference to EnemyController, but I’m sure you can figure that out, and I wrote the above assuming you wanted to build your list of player objects on the server only. You’d write it a bit different if you’re trying to do that on the clients instead.