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

Linking two gameobjects on client and server

Discussion in 'Multiplayer' started by Lordinarius, Sep 12, 2015.

  1. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    94
    Hi.

    In my game i spawn objects manually. The prefab has network idendity and network transform components on it.

    in my test scene. when i press the play this prefab being spawned on client and host independent from network.
    Then i connect Client to the Host. Now the thing that i want to do is linking these gameobjects between host and client. How ?

    Thanks

    Test project is included at the attachment
     

    Attached Files:

  2. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    94
    Ok i've achived this :)

    Code (CSharp):
    1. public class NetTest : NetworkManager {
    2.  
    3.     public GameObject ccaa;
    4.     GameObject pop;
    5.     public bool usclient;
    6.    
    7.     const short MyBeginMsg = 1002;
    8.     void Start(){
    9.        
    10.         pop = (GameObject)Instantiate(ccaa, Vector3.zero, Quaternion.identity);
    11.         if (Application.isEditor) {
    12.            
    13.         }
    14.  
    15.     }
    16.     public void Init()
    17.     {
    18.  
    19.     }
    20.     public override void OnStartHost(){
    21.         //base.OnStartHost();
    22.        
    23.     }
    24.     //Called on the server when a new client connects.
    25.     public override void OnServerConnect(NetworkConnection conn) {
    26.         //base.OnServerConnect(conn);
    27.         Debug.Log(conn.connectionId + " : isConnected");
    28.     }
    29.    
    30.  
    31.     //Called on the server when a client adds a new player.
    32.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    33.     {
    34.         Debug.Log(conn.connectionId + " : is requested spawn with " + playerControllerId + " playerControllerId" );
    35.         NetworkServer.AddPlayerForConnection(conn, pop, playerControllerId);
    36.         //NetworkServer.ReplacePlayerForConnection(conn,pop,playerControllerId);
    37.     }
    38.    
    39.     //Called on the client when connected to a server.
    40.     public override void OnClientConnect(NetworkConnection conn)
    41.     {
    42.         ClientScene.Ready(conn);
    43.         //base.OnClientConnect(conn);
    44.         if (!Application.isEditor) {
    45.             //ClientScene.AddPlayer
    46.            
    47.             ClientScene.RegisterPrefab(pop,SpawnHandler,UnSpawnHandler);
    48.             ClientScene.AddPlayer(conn,0);
    49.             //base.OnClientConnect(conn);
    50.             //ClientScene.SetLocalObject(pop.GetComponent<NetworkIdentity>().netId,pop);
    51.             //NetworkServer.ReplacePlayerForConnection(conn,pop,3);
    52.         }
    53.     }
    54.    
    55.     public override void OnClientDisconnect(NetworkConnection conn)
    56.     {
    57.         //StopClient();
    58.     }
    59.         private  GameObject SpawnHandler(Vector3 position, NetworkHash128 assetID)
    60.     {
    61.         Debug.Log("Called Spawn Handler");
    62.         return pop;
    63.     }
    64.    
    65.     private  void UnSpawnHandler(GameObject gameObject)
    66.     {
    67.         Debug.Log("Called UnSpawn Handler");
    68.     }
    69.  
    70.    
    71.     public override void OnServerReady(NetworkConnection conn)
    72.     {
    73.         Debug.Log(conn.connectionId + " : is Ready");
    74.     //NetworkServer.SetClientReady(conn);
    75.     }
    76.    
    77.    
    78. }