- Home /
How Do I Enable a GameObject upon Button Press?
I want a green spotlight thing to enable when I press 'tab'. Here is the code I have so far. This script is on the green spotlight itself.
using UnityEngine;
using System.Collections;
public class SelectPlayer : MonoBehaviour
{
// Use this for initialization
void Start ()
{
if(Input.GetKey(KeyCode.Tab))
{
gameObject.SetActive(true);
}
}
// Update is called once per frame
void Update ()
{
}
}
Nothing happens when I press tab. I've tried setting the GameObject on and off manually in-client before testing, and either way it doesn't work. Thanks in advance!
Answer by YoungDeveloper · Nov 27, 2014 at 05:24 PM
You must process input in Update, start is called only once.
void Update () {
if(Input.GetKeyDown(KeyCode.Tab)){
gameObject.SetActive(!gameObject.activeSelf);
Debug.Log("set");
}
Debug.Log ("Update is called");
}
This will set gameobject active state to opposite. Notice, that you will be able only to disable the gameobject, as disabled components aren't updated, "Update is called" wont be called. So in this case there's really no way of re-enabling the gameobject from same gameobject.
The solution is to process SetActive() from other gameobject with active state is always true. It can either be parent or completely separate gameobject.
Okay. I'm new to coding, so what you said in about the SetActive makes little sense to me :(. I know its asking for a lot, but could we do something where the object is turned off, i press tab, and then the object appears and fades out after a couple of seconds? Or would it be simpler just to do what you said? If so, please explain like I'm five. Thanks so much :)
OR!: Could we just have the object fade in our out when tab is pressed?
In most cases, disabling whole gameobject isn't needed. You could just toggle simple bool and then check the state. Try and run this.
bool flag = true;
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab)){
flag = !flag; //set flag to opposite value
}
if(flag){
Debug.Log ("yes");
}
else{
Debug.Log ("no");
}
}
Generally disabling a whole GameObject is more efficient the bool checking.
Yes, but he needs extra functionally even if one module is disabled.
Answer by Kiwasi · Nov 27, 2014 at 05:19 PM
You can't have a GameObject set itself to active. This is because Unity does not do any call backs on inactive GameObjects.
Put this script on another GameObject that is active in the scene. Pass it a referece in the inspector to you inactive object, and it will work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613172600im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to set game objects as inactive via button 1 Answer
Multiple Cars not working 1 Answer
error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 3 Answers
Load a GameObject that is outside of script and set it active at the same time 1 Answer
Distribute terrain in zones 3 Answers