Search Unity

UNet Commands sends twice

Discussion in 'UNet' started by Slack43, Aug 9, 2018.

  1. Slack43

    Slack43

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

    Slack43

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

    Joe-Censored

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

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    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:
    11,847
    And you don't call "CmdPlaceTrap" anywhere else in your project?
     
  6. Slack43

    Slack43

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