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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Randomly Flash Buttons

Discussion in '2D' started by doduarrow, Dec 20, 2015.

  1. doduarrow

    doduarrow

    Joined:
    Dec 20, 2015
    Posts:
    4
    i want to make one simple game.
    it is very easy.
    there are 2 button left and right bottom
    randomly one of this two buttons apear if you press on time you earn 1 points if you cant press you lost -2 point.
    if you press wrong button you lost -2 points too.
    how can i make this
     
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Code (CSharp):
    1. public gameObject myButton1;
    2. public gameObject myButton2;
    3. private bool processButton1 = true;
    4. private bool processButton2 = true;
    5.  
    6. void Awake(){
    7.   myButton1.setActive(false); //INITIALIZE HIDDEN
    8.   myButton2.setActive(false);
    9. }
    10.  
    11. void Update(){
    12.   if(processButton1){ //IF WE SHOULD BE CHECKING
    13.     if(Random.value < 0.5f){ //PICK A RANDOM NUMBER BETWEEN 0.0f to 1.0f
    14.       processButton1 = false; //STOP CHECKING
    15.       myButton1.setActive(true); //SHOW THE BUTTON
    16.       StartCoroutine(DelayHideButton1()); //AFTER A WHILE HIDE THE BUTTON
    17.     }
    18.   }
    19.   if(processButton2){
    20.     if(Random.value > 0.5f){
    21.       processButton2 = false;
    22.       myButton2.setActive(true);
    23.       StartCoroutine(DelayHideButton2());
    24.     }
    25.   }
    26. }
    27.  
    28. IEnumerator DelayHideButton1() {
    29.   yield return new WaitForSeconds(0.5f); //WAIT HALF A SECOND
    30.   myButton1.setActive(false); //HIDE THE BUTTON
    31.   processButton1 = true; //AGAIN RUN Update() TO SEE IF WE SHOULD SHOW IT AGAIN
    32. }
    33.  
    34. IEnumerator DelayHideButton2() {
    35.   yield return new WaitForSeconds(0.5f);
    36.   myButton2.setActive(false);
    37.   processButton2 = true;
    38. }
     
    doduarrow likes this.
  3. doduarrow

    doduarrow

    Joined:
    Dec 20, 2015
    Posts:
    4
    ty mate.
    i try it.
    i am very new of coding. actualy i am very old to learn (30 years old). but i want something and i realize i have start somewhere.
    i am very good at make terrain but very bad at coding.
    ty reply my message