Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Problem] Reference to player gameObject needed

Discussion in 'Multiplayer' started by eyalandron, Jul 9, 2018.

  1. eyalandron

    eyalandron

    Joined:
    Jan 10, 2018
    Posts:
    3
    So I've tried using onClientConnect in order to have this:
    Code (CSharp):
    1.   public override void OnClientConnect(NetworkConnection conn) {
    2.          ClientScene.Ready(conn);
    3.          Object[] allObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject));
    4.          foreach (GameObject go in allObjects) {
    5.              if (go.tag != "NetworkPlayer") {
    6.                  EnemyController.AddPlayer(go);
    7.              }
    8.          }
    9.          Debug.Log("test");
    10.          //ClientScene.AddPlayer(0);
    11.      
    12.      }
    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 :O
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why not just have the Player objects add themselves when they start?

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.     if (isServer)
    5.     {
    6.         EnemyController.AddPlayer(gameObject);
    7.     }
    8. }
    9.  
     
  3. eyalandron

    eyalandron

    Joined:
    Jan 10, 2018
    Posts:
    3
    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?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.