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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Button Multiple Press Leads to Multiple Execution of Function

Discussion in 'UGUI & TextMesh Pro' started by wkpk11235, Apr 13, 2018.

  1. wkpk11235

    wkpk11235

    Joined:
    Apr 6, 2018
    Posts:
    2
    I have been using Unity's UI buttons to make a clickable object. I gave it a function.

    --snip--
    Code (CSharp):
    1. public void Click(){
    2.     if (clickcount == 0) {
    3.         clickd = true;
    4.         clickcount += 1;
    5. }
    --snip--


    This function links to this other function in the same object:
    --snip--
    Code (CSharp):
    1. void OnTriggerStay2D(Collider2D other){
    2.     if (clickd && other.gameObject.CompareTag ("touchable")) {
    3.             other.gameObject.GetComponent<CowRules> ().DEATH ();
    4.     }
    5. }
    --snip--

    then the DEATH() part sets clickcount to 0 and clickd to false.


    But then, when I clicked the button twice or more, the button's script executes twice and sets clickd to true twice.

    How do I fix this? Thanks.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That makes sense.. What is it you want it to do, instead?

    I mean, can they only click once while in the trigger?
    What is the purpose of click count?
     
  3. wkpk11235

    wkpk11235

    Joined:
    Apr 6, 2018
    Posts:
    2
    The click count's purpose is to set clickd to true only once, but right after the DEATH() sets clickd to false, the button's second click (which was stacked) sets clickd to true again, which gives an unintended result.

    Edit: I solved the problem using another solution. Thanks for the help!
     
    Last edited: Apr 14, 2018