Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Problem] Reference to player gameObject needed

Discussion in 'Connected Games' 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:
    3,774
    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:
    3,774
    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.