- Home /
Script variable doesn't appear in UI's inspector
Hello Unity Community!,
I'm trying to turn off a boolean variable in UI's inspector. I'm using Unity 5, and I don't know what I'm doing wrong. What should I do?
This is the variable.
#pragma strict
var MainMenu : GameObject;
var MainMenuON : boolean = true;
function Start () {
}
function Update () {
if(MainMenuON){
MainMenu.SetActive (true);
}
else if(!MainMenuON){
MainMenu.SetActive (false);
}
}
where are you declaring it? please show the entire script
you have to create a public function to make your boolean false on the button click. I guess you know how to do that.
Its kind of wired that you can set a bool from another scirpt like useGUILayout in the screenshot but not your own variable of type bool.
Answer by KdRWaylander · Apr 28, 2015 at 02:42 PM
Hi,
Change:
var MainMenuON : boolean = true;
To:
public var MainMenuON : boolean = true;
Even if JS assumes it's a public one when you declare a variable, you have to specify it to actually make it appear in the hierarchy.
On the contrary, if you wanna hide in inspector a (specificly) public variable, you have to add @HideInInspector
in front of your variable !
@HideInInspector public var myBool : boolean = true;
EDIT: And if I may, to simplify your code, instead of:
if(MainMenuON){
MainMenu.SetActive (true);
}
else if(!MainMenuON){
MainMenu.SetActive (false);
}
you can only use:
MainMenu.SetActive (MainMenuON);
Answer by brunopava · Apr 28, 2015 at 02:44 PM
Also, you need to toggle your boolean to false and true. Like this:
public function Toggle()
{
if(MainMenuON){
// if its active, then deactivate
MainMenuON = false;
MainMenu.SetActive (false);
}
else{
// if its deactivated, then activate
MainMenuON = true;
MainMenu.SetActive (true);
}
}
Then just choose this function on the inspector.
Your answer
Follow this Question
Related Questions
Notes or Bubble Window for Variables in Inspector Giving Help? 3 Answers
Adding variables to inspector strings 1 Answer
A code to reset a variable after x time if another button is not pressed. Help! 2 Answers
Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers
Unity UI 4.6 Canvas Enable/Disable make Accessible 2 Answers