Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party (Mirror)NetworkServer.Spawn() ONLY works on host

Discussion in 'Multiplayer' started by maorachow, Aug 4, 2023.

  1. maorachow

    maorachow

    Joined:
    Dec 12, 2021
    Posts:
    1
    I was trying to make players spawn objects on the server. I tested the code but I found that when I joined the gameplay scene as a host, the object-creating function worked well, but when I join the scene as a client, the object-creating function was broken and no objects are spawned. There were no log errors but the objects just can't spawn. In the client's camera view I saw that the host player successfully created the object but when the client player called the same command, It didn't work. I wonder why ONLY hosts can use command to spawn objects and clients can't.
    Code (CSharp):
    1. [Command]
    2.     public void CmdCreateBody(){
    3.           if(snakeBodies.Count>0){
    4.    
    5.              GameObject a=Instantiate(snakeBodyPrefab,snakeBodies[snakeBodies.Count-1].GetChild(0).GetChild(0).position,transform.rotation);
    6.              a.GetComponent<SnakeBodyMove>().ownerPlayer=this.gameObject;
    7.                 a.transform.position=snakeBodies[snakeBodies.Count-1].GetChild(0).GetChild(0).position;
    8.                 a.transform.rotation=transform.rotation;
    9.            snakeBodies.Add(a.transform);
    10.            NetworkServer.Spawn(a,a.GetComponent<NetworkIdentity>().assetId);
    11.             createBody=false;  
    12.             }else{
    13.      
    14.                 GameObject a=Instantiate(snakeBodyPrefab,transform.GetChild(0).position,transform.rotation);
    15.                  a.GetComponent<SnakeBodyMove>().ownerPlayer=this.gameObject;
    16.                 a.transform.position=transform.GetChild(0).position;
    17.                 a.transform.rotation=transform.rotation;
    18.             snakeBodies.Add(a.transform);
    19.             NetworkServer.Spawn(a,a.GetComponent<NetworkIdentity>().assetId);
    20.             createBody=false;  
    21.             }
    22.        
    23.     }