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.
GameObject object = Instantiate (objectPrefab, castPosition, Quaternion.identity);
object.GetComponent().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.
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.
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!