GUI Button not doing what I told it to do.

Hi! I’m kind of new to scripting and working with unity so I thought I might ask for help here.

I’ve written a simple script which should generate a button which when clicked will generate a random number and display it in the middle of the screen. Everything works except showing that number in the middle of the screen.

This is the code I’ve got so far:

#pragma strict

var btnTexture : Texture;
var number : int;

	function Start() 
	{
	
	number = Random.Range(1,999999999);
	
	}




	function OnGUI() 
	{

		if (GUI.Button(Rect(85,50,150,50),"Get Random Number"))
	 		GUI.Label(Rect(85, 60, 150, 50), "The number is" + number);
	 		
	}

Computers always do exactly what you tell them to do. They don’t necessarily do what you want them to do, but they can’t read your mind. In this case, it displays the label when you click the button, which–since OnGUI is immediate mode and typically runs twice per frame–means it disappears as fast as it’s displayed. You’ll have to tell it to do something else if you want different behavior; I’d suggest making a boolean variable which controls whether the label is displayed, and clicking the button will turn the boolean from false to true.

–Eric

Oh, okay. And how do I do that?

  #pragma strict
     
    var btnTexture : Texture;
    var number : float;
     
        function Start()
        {
       
        number = Random.Range(1,999999999);
       
        }
     
     
     
     
        function OnGUI()
        {
     
            if (GUI.Button(Rect(85,50,150,50),"Get Random Number"))
                {
Debug.Log("the number is"+number );
		}
        }

use this^^jst u get random value in console window but not in editor window.u need to modify …for ur use i have jst helped!!!as basic

Yeah thanks, the value shows in the console but I need it on the screen when I click the button and I have no idea how to do that…

Should it show up all the time or only after you pressed your button at least once?

now its done u can see in game window now in box

  var btnTexture : Texture;

    var number : float=0;
     function Start()

        {
        }

        function OnGUI()

        {
   if (GUI.Button(Rect(85,50,60,50),"Get Random Number"))

                {
     number = Random.Range(1,999999999);
               }
     GUI.BOX(Rect(255,50,150,50),"the number iS"+number ); 
        }

Make a global boolean:

private var showBox = false;

And use that to display the box or not inside OnGUI:

    if (showBox) {
        // display the box
    }

–Eric

Thank you!