Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 10:47 PM.

  1. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    55
    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:
    55
    I didn't specify that the NetworkServer.Spawn executes twice.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    2,966
    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:
    55
    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:
    2,966
    And you don't call "CmdPlaceTrap" anywhere else in your project?
     
  6. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    55
    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.