Search Unity

Player call command to spawn an object. How to tag the object with playerID

Discussion in 'Multiplayer' started by fgggenie, May 19, 2017.

  1. fgggenie

    fgggenie

    Joined:
    May 14, 2017
    Posts:
    2
    Hi

    Here is the situation.
    I want to have my players to be able to instantiate an object. The object is in networkManager's spawn list and will sync its transform. So I end up using a script on player that do [Command] cmd method and pass in player's id as a parameter. The object is also taking that player's id and store it in its gameObject script. But I find that when player is a client. The object cannot get the player's id.

    [Command]
    private void CmdCast (NetworkHash128 playerID)
    {

    GameObject object = Instantiate (objectPrefab, castPosition, Quaternion.identity);
    object.GetComponent<ObjectController>().Initialize(playerID); // tag the object with player's id

    NetworkServer.Spawn(object);
    }
    However this doesn't work on client, don't understand why


    It might sound awkward, but what I am trying to do it to tag an object (spawned by the server) with its creater (player). So that object can invoke some callback functions to that exact player.
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    When referencing networked objects. Use NetworkInstanceId instead of NetworkHash128. That way you can use NetworkServer.FindLocalObject(urNetInstanceId); or ClientScene.FindLocalObject.
    But if you want the playerId variable to sync in the objectController class make sure you mark it with SyncVar.
     
  3. fgggenie

    fgggenie

    Joined:
    May 14, 2017
    Posts:
    2
    @TwoTen
    Thank you!
    I will use NetworkInstanceId. That was something I miss.
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Every NetworkIdentity (every networkedObject) has a NetID that is the same across all places. Server, client etc. It's therefor very useful for these things. Good luck!