- Home /
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 :)
Answer by Eric5h5 · Mar 09, 2011 at 06:53 AM
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
Answer by Ashkan_gc · Mar 09, 2011 at 07:49 AM
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.
Your answer
Follow this Question
Related Questions
What is the name of the guistyle to draw the preview buttons? 1 Answer
Compiler Error, how to fix? 1 Answer
Lining up GUI 1 Answer
How to make a global settings Editor Window for my game 0 Answers
EditorGUI how does it work? 2 Answers