Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

onGui - GUI.Button hits multiple times

Discussion in 'Immediate Mode GUI (IMGUI)' started by Nar-Tak, Sep 4, 2014.

  1. Nar-Tak

    Nar-Tak

    Joined:
    Sep 4, 2014
    Posts:
    3
    Hey!
    Sorry, I am quite new and have a problem I am too stupid to solve.

    in onGUI i have a button which uses "Instantiate" to spawn an object.
    While it works perfectly fine within the unity editor when I build the project and click the same button it causes multiple objects to spawn.

    I assume it is because onGUI is called multiple times per frame and the "if (GUI.Button...) remains 'true', causing "Instantiate" to be called more than once.

    How would I prevent this?

    Code (CSharp):
    1.  
    2. void OnGUI ()
    3. {
    4.     //stuff
    5.     if (GUI.Button (new Rect (20, 75, 100, 30), "Singleplayer"))
    6.     {
    7.         Vector3 spawnpos = new Vector3(250f, 5f, 250f);
    8.         Instantiate(PlayerStartMeUp,spawnpos,Quaternion.identity);
    9.     }
    10.     //also stuff
    11. }
    12.  
    Sorry, my english isn't too good.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    That's only the case for GUI.RepeatButton. GUI.Button returns true just once, and can only be true again when the mouse button is pressed and released again. Maybe you have the same script attached to multiple objects?

    --Eric
     
  3. Nar-Tak

    Nar-Tak

    Joined:
    Sep 4, 2014
    Posts:
    3
    Thank you for answering.
    I considered your idea but it is not the case. The script is only in one object. It also works perfectly fine in the Unity Editor. (If I run the project in there). Only when I build the project and run the .exe file the problem occurs.

    But if you say GUI.Button is only true once per click then I have no clue what the cause is. :(

    I had the idea because this is also the case with another function such as rotating an object by 90 degrees 'per click'. It is turned 180 degrees most of the time as if the mouse is clicked twice. But I assume this is a different story then (since it's not GUI.Button but GetButtonDown).

    Code (CSharp):
    1.            
    2. if (Input.GetButtonDown ("Fire2"))
    3. {
    4.       MyCurConstruction.transform.Rotate(0,45,0);
    5.       Vector3 vecRound = MyCurConstruction.transform.eulerAngles;
    6.       vecRound.y = Mathf.Round(vecRound.y);
    7.      MyCurConstruction.transform.eulerAngles = vecRound;
    8. }
    9.  
     
  4. Nar-Tak

    Nar-Tak

    Joined:
    Sep 4, 2014
    Posts:
    3
    Okay, I think I am an idiot.
    The problem was that I had no camera in place and it simply looked like the UI hat multiple UI elements placed above each other. Once I added a camera it didn't seem to happen anymore.
    I was not aware that the UI behaves that way but I guess it makes sense...

    Thanks for pointing me to the right door. I was stuck at trying to figure out why the button would be 'clicked' multiple times while it actually didn't really happen. Also, sorry for the stupid question. I'll be back with more later... :p