- Home /
Object won't reactivate or activate with button press.
I am using a script to activate an object when I press "b".If the object is active it will deactivate if I press be but will never activate or reactivate. What am I missing here? I put the object in the variable buildMenu. It is a simple plane with a text on it and box collider.
var buildMenu : GameObject;
var menuActive = false;
function Start()
{
menuActive = false;
}
function Update ()
{
if (Input.GetKeyDown ("b"))
{
if (menuActive == false)
{
buildMenu.SetActive(true);
menuActive = true;
Debug.Log("Menu Active");
}
if (menuActive == true)
{
buildMenu.SetActive(false);
menuActive = false;
Debug.Log("Menu Not Active");
}
}
}
Answer by zharik86 · Sep 02, 2014 at 07:46 AM
Certainly, at you it to turn out nothing because two of your conditions are satisfied one after another in one frame. I will a little correct your script:
function Update () {
if (Input.GetKeyDown (KeyCode.B)) {
if (menuActive == false) {
buildMenu.SetActive(true);
menuActive = true;
Debug.Log("Menu Active");
} else {
buildMenu.SetActive(false);
menuActive = false;
Debug.Log("Menu Not Active");
}
}
}
I hope that it will help you.
Answer by Addyarb · Sep 02, 2014 at 07:51 AM
If the script is on the object itself, it will not reactivate because the very script that you are trying to access is not active.
You must access this script from another script.
Put this script on another GameObject in your scene, and activate it using references. (i.e. put it on a parent of the object, or find the object using GameObject.Find (" ").GetComponent();
Your answer
Follow this Question
Related Questions
How to activate a script from another script at runtime 1 Answer
How to make object move and rotate 1 Answer
enable scripts/ components on other gameobjects for a different gameobject? 1 Answer
Hide/Show a game object and activate/deactivate a script of another game object with one script 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers