Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not able to display GUIBOX..when user clicks on Right button and if original answer

Discussion in 'Scripting' started by sidhesh, Dec 6, 2012.

  1. sidhesh

    sidhesh

    Joined:
    Nov 6, 2012
    Posts:
    51
    i want display GUIBOX if certain condition meets...u can see in my code if(rand4 == rand3)
    i am displaying guibox and then i am calling start() function...

    but whenever i run my program in unity and above condition meets then it directly calls start() function...
    but i want first to display guibox or label then adding scores and then to call start()....plz look at my code...
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4.  
    5.  
    6.  var rand1:int;
    7.  
    8. var rand2:int;
    9.  
    10. var rand3:int;
    11.  
    12. var rand4:int;
    13.  
    14.  var ran:int;
    15.  
    16. var currTry:int = 0;   //Increment when click on button
    17.  
    18. var maxTry:int = 20;
    19.  
    20. var score : int = 0;
    21. var guiScore : GUIText;
    22.  
    23.  function Awake()
    24.  {
    25.  
    26.  
    27.  
    28.  }
    29.  
    30.  function Start()
    31.  {
    32.  ran = Random.Range(0,3);
    33.  
    34. rand1 = Random.Range(0, 20);//Random number1
    35.  
    36. rand2= Random.Range(0,20);//Random number2
    37.  
    38. rand3= Random.Range(0,40);//Random number3
    39.  
    40. rand4 = rand1 + rand2; //Internally calculation
    41.  
    42. Debug.Log(rand4);
    43.  
    44. guiScore.text = "Score: 0";
    45.  
    46.  }
    47.  
    48.   function Update () {
    49.   }
    50.  
    51.   function Score()
    52. {      
    53.  
    54.    if(GUI.Button(Rect(10,70,50,30),"right"))//GUI button for right
    55.  
    56. {
    57.  
    58. currTry++;    //Increment Here
    59.  
    60.   if(rand4 == rand3)
    61.  
    62.   {
    63.       GUI.Box(Rect(120,70,50,30),"ITS WORKING");
    64.      score += 10;
    65.         guiScore.text = "Score: " + score;      
    66.   Start();
    67.   //Awake();
    68.     //first();
    69.   }
    70.  
    71.  
    72.  
    73.   else
    74.        {
    75.         //GUI.Box(Rect(120,70,50,30),"ITS NOT WORKING");
    76.        Destroy(this.gameObject);
    77.        }
    78.  
    79.   }//for
    80.  
    81.     if(GUI.Button(Rect(90,70,50,30),"wrong"))//GUI button for wrong
    82.  
    83.   {
    84.  
    85. currTry++;      //Increment Here Again
    86.  
    87.  if(rand4 != rand3)
    88.   {  
    89.        score += 10;
    90.         guiScore.text = "Score: " + score;  
    91.         GUI.Box(Rect(120,70,50,30),"ITS WORKING");
    92.     Start();
    93.  
    94.     // Awake();     //first();
    95.      
    96.  
    97.    }
    98.  
    99.  else
    100.      {
    101.       GUI.Box(Rect(120,70,50,30),"ITS NOT WORKING");
    102.     Destroy(this.gameObject);
    103.  
    104.      }
    105.      
    106.   }
    107.     }
    108.    
    109.    
    110.     function OnGUI()
    111.  
    112. {
    113.  
    114.  
    115.  
    116. if(currTry < maxTry)   //check if we
    117.  
    118.  
    119.  
    120. {
    121.          //GUI.Button(Rect(150,70,50,30),"##");
    122.          //GUI.Button(Rect(200,70,50,30),"**");
    123.  
    124. GUI.Box (Rect (30,5,25,30), rand1.ToString());//first random number
    125.  
    126.  
    127.  
    128. GUI.Box (Rect (90,5,25,30), rand2.ToString());//second random number
    129.  
    130.  
    131.  
    132. GUI.Box (Rect (150,5,25,30), rand3.ToString());//third random number
    133.  
    134.  
    135.  
    136. //GUI.Box (Rect (60,5,25,30),"+");//for +symbol
    137.  
    138. GUI.Box (Rect (60,5,25,30),GetRandomOperator());
    139.  
    140. GUI.Box (Rect (120,5,25,30),"=");//for equals to
    141.  
    142.  
    143.  
    144.     Score();
    145.     }
    146. else
    147.  
    148. {
    149.  
    150. Debug.Log("-----------------------------------MAX TRY COMPLETED-------------------------------");
    151.  
    152. ///Do Something else after 20 Try Done
    153.  
    154. }
    155.  
    156.  
    157.  
    158.   }
    159.  
    160.   function GetRandomOperator()
    161.  
    162. {
    163. //var ran:int;
    164.  //ran = Random.Range(0,4);
    165.  
    166. switch(ran)
    167.  
    168. {
    169.  
    170.  case 0:
    171.  
    172.    return "+";
    173.  
    174.  
    175.  
    176.  case 1:
    177.  
    178.   return "-";
    179.  
    180.  
    181.  
    182.  case 2:
    183.  
    184.    return "*";
    185.  
    186.  
    187.  
    188. }
    189.  
    190.  
    191.  
    192. }
    193. /*
    194. var rand1:int;
    195. var rand2:int;
    196. var rand3:int;
    197. var rand4:int;
    198.  
    199. function Start()
    200.  
    201. {
    202.  
    203.  
    204.  
    205. rand1 = Random.Range(0, 10);//Random number1
    206.  
    207. rand2= Random.Range(0,10);//Random number2
    208. rand3= Random.Range(2,19);//Random number3
    209.  
    210.  
    211.  
    212. rand4 = rand1 + rand2; //Internally calculation
    213. Debug.Log(rand4);
    214.  
    215. }
    216.  
    217. function OnGUI()
    218. {
    219.  
    220. GUI.Box (Rect (30,5,25,30), rand1.ToString());//first random number
    221.  
    222. GUI.Box (Rect (90,5,25,30), rand2.ToString());//second random number
    223.  
    224. GUI.Box (Rect (150,5,25,30), rand3.ToString());//third random number
    225.  
    226. GUI.Box (Rect (60,5,25,30),"+");//for +symbol
    227.  
    228. GUI.Box (Rect (120,5,25,30),"=");//for equals to
    229.  
    230. if(GUI.Button(Rect(10,70,50,30),"right"))//GUI button for right
    231. {
    232.   if(rand4 == rand3)
    233.   {
    234.  
    235.   Start();
    236.   }
    237.  
    238.   else
    239.   Destroy(this.gameObject);
    240.  
    241.   }
    242. if(GUI.Button(Rect(90,70,50,30),"wrong"))//GUI button for wrong
    243. {
    244.  if(rand4 != rand3)
    245.  Start();
    246.  
    247.  else
    248.   Destroy(this.gameObject);
    249.  
    250.   }
    251.   }
    252.  
    253. */
    254.  
    255.  
    256.  
     
    Last edited: Dec 7, 2012
  2. Whatbadguy

    Whatbadguy

    Joined:
    Apr 10, 2011
    Posts:
    165
    Love to help, I just don't understand your wordception..
     
  3. sidhesh

    sidhesh

    Joined:
    Nov 6, 2012
    Posts:
    51
    Whatbadguy plz look at my post i hv edited it....
     
    Last edited: Dec 7, 2012
  4. sidhesh

    sidhesh

    Joined:
    Nov 6, 2012
    Posts:
    51
    plz help .....