Search Unity

Unity 5.1 networking, Cant get [Command] to work :(

Discussion in 'Multiplayer' started by LiOn0X0HeaRt, Jun 22, 2015.

  1. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    Hi
    i am using the latest unity version 5.1.1 f1
    and i have been stuck for ages trying to solve a problem which seems to be simple to everyone

    i can't spawn objects to the server no matter what
    this is the code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. public class InsTest : NetworkBehaviour {
    5. public GameObject testIns;
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.     if(Input.GetKeyDown(KeyCode.I))
    14.     {
    15.     CmdTestIns();
    16.  
    17.  
    18.     }
    19.     }
    20.  
    21.  
    22.     [Command]
    23.     void CmdTestIns()
    24.     {
    25.     GameObject tmp = Instantiate(testIns,Vector3.zero,Quaternion.identity) as GameObject;
    26.     NetworkServer.Spawn(tmp);
    27.     }
    28. }
    29.  

    its pretty simple and neat

    i have all the components net manager and network identity
    and i have add the object to the spawn list

    on this object that has this script there is only network identity
    i have debugged it outside the game and inside and they all run

    first if i was the host the object gets instantiated but only one can see it which is the host

    second if i was the client nothing gets instantiated on the host or the client

    i have followed the steps very carefully of many tutorials on youtube and unity fourm/answers
    this bug just refuses to get solved

    UPDATE****************

    I changed the script from MonoBehavior to NetworkBehavior

    and kept everything the same i faced a problem telling me your object doesn't have network identity

    the thing is that object was the NetworkManager and NetworkManagerHud components

    so i removed the script made another object and added network identity with the
    instantiate script

    it worked with the host if the host instantiates everyone gets it

    but if i try to instantiate from the player i get this error
    unity trying to send a command for non local player

    and it doesn't work i debugged it this code stops the instantiating
    here is a picture of the gameobject



    [img=http://s22.postimg.org/lpoggvzml/locl.jpg]

    UPDATE***********************************************************************
    This error is getting on my nerves
    unity trying to send a command for non local player

    its local player i am 100% sure the instantiate code is disabled if not local
    by disabling it as a prefab and enabling it on start i debugged it and i am
    100% sure every instantiate script works only on the machine locally

    this issue unity trying to send a command for non local player
    combined with the fact that isLocalPlayer doesn't work as it supposed to
    its false all the time no matter what it doesn't recognize that i am running the game locally

    they indicate that there is something wrong with unity
     
    Last edited: Jun 22, 2015
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I am moving this to the correct forum. Teaching is for discussing tutorial and and lessons.
     
  3. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Commands work on local player objects (e.g. the one you have put in the prefab field of NetworkManager). Your client has to have "localAuthority" over that object to send a command. Not every gameobject will send commands.

    If you start the server and connect a client, a prefab will be instantiated. That should work. Your "ins"-object is a scene object and not a local player object.
     
  4. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    I tried to add the InsTest script to the players
    the players are auto instantiated by the NetworkManager
    i got the same errors
    i checked if i had any reference for InsTest and i didn't find any
    InsTest is being instantiated with the players


    i still get the same error

    player is not local and

    this issue unity trying to send a command for non local player
     
  5. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    Hey i solved the issue
    turned out my steps were fine but there was some bug if you enable a component after instantiating it
    that component wont work
    basically i was disabling InsTest and enabling it on the start of the game so the Host will only see his insTest enabled and the clients will only see theirs
    i checked if they were true by debugging and they were true for everyone(i used an asset to debug after build)
    there were no issues about any local stuff and InsTest was working for everyone

    the only issue was if you disable a component and try to enable it inside the game
    it will be enabled but it simply wont work except for the host

    turns out that unity networking system doesn't work like photon's

    interesting

    thanks for helping you were a big help :)
     
  6. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    A bit old post but maybe it will help to future users use this
    Code (CSharp):
    1. // Update is called once per frame
    2.    void Update () {
    3.   if (!isLocalPlayer) {
    4.            return;
    5.         }
    6.    if(Input.GetKeyDown(KeyCode.I))
    7.    {
    8.     CmdTestIns();
    9.  
    10.  
    11.    }
    12.    }
     
  7. TheDarkVoice

    TheDarkVoice

    Joined:
    Jan 31, 2016
    Posts:
    3
    MFKJ, Thanks a lot mate! I've had this problem for over a week, and I was really starting to get mad, but you realy helped me. Thanks!