Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    4,456
    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:
    4,456
    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.