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.
#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
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 );
}