Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NetworkServer.Spawn only instantiates object on host (SOLVED)

Discussion in '5.1 Beta' started by CineTek, May 31, 2015.

  1. CineTek

    CineTek

    Joined:
    Jul 12, 2013
    Posts:
    98
    Hey I´ve been playing around with the new UNET docs and sample projects for the last couple of days and decided to write a little lobby based 2D shooter this weekend.

    One thing I can not figure out is why the bullets are only instantiated on the server if my host is the one shooting and not when anyone else is shooting. The Bullets are visible on the local client(s) but not on the server basically. The only exception is if the host is shooting them, then they are correctly instantiated and tracked by the networkTransform component.

    My setup:

    1. "Bullet" prefab has a NetworkIdentity + NetworkTransform component and has been added to the "Registered Spawnable Prefabs" list.
    2. I am calling "NetworkServer.Spawn()" in my scripts as a command:

    Code (CSharp):
    1. void Update ()
    2.     {
    3.  
    4.         if (pc.isLocalPlayer)
    5.         {
    6.             Vector3 mouseScreen = Input.mousePosition;
    7.             Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
    8.  
    9.             if (Input.GetButtonDown("Fire1"))
    10.             {
    11.                 /*
    12.                 object[] data = new object[3];
    13.                 data[0] = bulletSpawn.position;
    14.                 data[1] = transform.parent.rotation;
    15.                 data[2] = mouse;
    16.                  */
    17.  
    18.                 CmdShoot(bulletSpawn.position, transform.rotation);
    19.             }
    20.         }
    21.            
    22.  
    23.     }
    24.  
    25.     [Command]
    26.     void CmdShoot(Vector3 pos, Quaternion rot)
    27.     {
    28.        
    29.          GameObject clone = (GameObject)Instantiate(bulletPrefabs[0], pos, rot);
    30.          
    31.          NetworkServer.Spawn(clone);
    32.     }
    I´ve been looking through the "tank" and "2D shooter" sample to check that I have setup everything correctly but I can not find my mistake.

    Maybe the mistake is obvious and I´ve been searching in the wrong spots...
     
  2. CineTek

    CineTek

    Joined:
    Jul 12, 2013
    Posts:
    98
    It was able to solve this problem by making the script a "NetworkBehaviour" and putting it on the main player-object (which has the NetworkIdentity attached). Before it was directly added to the weapon-gameObject which is a child of the Player transform.