- Home /
Enable and disable one button from a Static Var?
Hello Everyone,
I'm trying to enable and disable one button from a Static var in other script.
please take a look to my script
Any advice is more than welcome.
Thanks in advance!
static var triggerGo : int; //Activate Tool.
static var cameraGo : int; //Activate Camera.
var Skin_tool : GUISkin;
var beep : AudioClip;
GUI.enabled = true;
function Update(){
if (animations.batteryGo == 1 ){
GUI.enabled = false;
if (animations.batteryBack == 1 ){
GUI.enabled = true;
}
}
}
function OnGUI (){
GUI.skin = Skin_tool;
if (GUI.Button(Rect(870,345,80,80),"")){
audio.PlayOneShot(beep);
triggerGo = 1; //Activate Tool.
cameraGo = 1; //Activate Camera.
}
}
@script RequireComponent(AudioSource)
As a general rule, never use "static". There are any number of long posts on here about it.
Check out unityGE$$anonymous$$S.com to learn how to very simply get at a variable from another script or object.
Or read the doco
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
So, don't use static in a situation like this. It is very easy to access the variables properly.
Answer by Flynn · Jan 26, 2013 at 09:11 AM
GUI.Enabled is intended to be used in the OnGUI() function -- The idea is that you set GUI.Enabled to false right before the code that draws the button, then set GUI.Enabled back to true right after that code. This will cause all of the code between when you set it to enabled/disabled will be disabled -- Perhaps this example will clear things up:
public function OnGUI()
{
//These buttons will be enabled
GUI.Enabled = true;
GUI.Button(...);
GUI.Button(...);
GUI.Button(...);
//These buttons will not be enabled
GUI.Enabled = false;
GUI.Button(...);
GUI.Button(...);
GUI.Button(...);
//This button will only be enabled if animations.batteryGo == 1
GUI.Enabled = animations.batteryGo == 1;
GUI.Button(...);
//These buttons will be enabled
GUI.Enabled = true;
GUI.Button(...);
GUI.Button(...);
GUI.Button(...);
}
Off topic, but is anyone else only seeing every fifth line number? Personally, I hate it, skipping every five line numbers doesn't save up any space in any meaningful way... overly fancy in my personal opinion. I am really glad to see we were given syntax highlighting though!
Yep, every fifth. I don't $$anonymous$$d it; I've worked with programs that show every line number and (when it's in the same font as the program itself) I find that very annoying. With just one every five I'm okay with it, especially when I recall that many, many questions are asked along with a 'error on line #X' and they don't bother to point out which line that is.
I could take it or leave it.