- Home /
 
               Question by 
               YayGee · Oct 08, 2013 at 02:54 PM · 
                javascriptrandomnumber  
              
 
              Adding GUI to this
I had to make a new question because I wasn't clear enough about what my question was the last time I asked this.
Hi. I watched a YouTube tutorial about random numbers. The creator of the video made a script that created random numbers in the video. But he didn't add any GUI text to it that would pop up on your screen.
My question is: How do I create a button that you can press. When you press it, you get a random number popping up on the screen with GUI text.
Here is the script he made:
 var randNumber : int;
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
         randNumber = Random.Range(1.0, 100.0);
         Debug.Log (randNumber);
         }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by clunk47 · Oct 08, 2013 at 02:54 PM
 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     string label;
     int num;
     
     void Awake()
     {
         ChangeNumber();
     }
     
     void ChangeNumber()
     {
         num = Random.Range(0, 100);
         label = num.ToString();    
     }
     
     void OnGUI()
     {
         if(GUI.Button(new Rect(0, 0, 128, 128), label))
             ChangeNumber();
     }
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Random.Range Generating Assigned Value 1 Answer
Randomly generated number 0 Answers
Adding GUI to this 1 Answer
Lottery system 1 Answer
Generate a random number that is different every time? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                