Create Button without If Statement

Hi all,

How do I declare Buttons and other GUI globally?

e.g.

var btn1:Button;

It gives me an error: BCE0018: The name "btn1" does not donate a valid type ("not found');

Well I need the button outside the OnGUI because I'm doing a racing game for Android. I'm using the buttons to accelerate/decelerate the car. And the variables I'm accessing are in another script.

My idea:

var btn1:Button;

function Update() {
 if(btn1) {
  //update acceleration
 }
}

function OnGUI() {
 //draw the GUI
}

I'm open to other workarounds or other solutions too.

Thank you in advance for your help :)

You don't use them globally; it doesn't work that way. GUI.Button has to be inside OnGUI or called from it. You probably want to read the GUI scripting guide: http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

you can not create a button by declaring a variable of type Button. you should have a Button call in OnGUI and set the input variable using it.

void OnGUI()
{
if (GUI.Button("accel"))
accel=true;
else
accel=false;
}

void Update()
{
if (accel==true)
{
//accelerate the car
}
}

in this way you can see which button is pressed in current frame or not. also you can use GUI Textures for buttons as unity itself uses them in their iphone tutorial.