Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer UNet Commands sends twice

Discussion in 'Connected Games' started by anloko123, Aug 9, 2018.

  1. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    64
    Hi, so i have this script that spawns a prefab whenever player presses "G".
    Before it worked correctly, now it does it twice.

    Script:
    Code (CSharp):
    1. [Command(channel = 1)]
    2.     public void CmdPlaceTrap(int obj)
    3.     {
    4.         if (isLocalPlayer)
    5.         {
    6.             if (trapsCount != 0)
    7.             {
    8.                 GameObject go_ = NetworkManager.singleton.spawnPrefabs[obj];
    9.                 GameObject goIns = (GameObject)Instantiate(go_, cursor.transform.position, this.transform.rotation);
    10.                 NetworkServer.Spawn(goIns);
    11.                 trapsCount--;
    12.                 RpcPlaySfx(goIns);
    13.             }
    14.         }
    15.     }
    Another script that triggers this action:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetKeyUp(KeyCode.G))
    4.         {
    5.             pc.CmdPlaceTrap(0);
    6.         }
    7.         if (Input.GetKeyDown(KeyCode.F))
    8.         {
    9.             Interact();
    10.         }
    11.     }
    I hope you understand.
     
  2. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    64
    I didn't specify that the NetworkServer.Spawn executes twice.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,593
    Are you absolutely sure you don't have two copies of the second script in your scene?
     
  4. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    64
    Yes i have put the name of the script in the Editor Search Bar and it showed only one object.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,593
    And you don't call "CmdPlaceTrap" anywhere else in your project?
     
  6. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    64
    I found out the problem. When using "Normal" mode in the inspector, i couldn't see the script. When i turned on the Debug mode i could see that the script was 2 times in the same object. I think that's an unity bug because in normal mode there was only one copy of the script.