- Home /
Question by
ProductDelta · Sep 28, 2015 at 06:37 AM ·
c#gameobjectenabledisabling
How to set game objects as inactive via button
I'm new to unity and c# but I want to know how to set a specific game object as inactive/active by pressing a button. Usually I do something like setting objects as .enabled = true/false but I can't do that with game objects that don't use using.UnityEngine.UI;
Comment
Answer by Cherno · Sep 28, 2015 at 11:23 AM
I'm gonna show you one way to do it, which is by adding a Listener to the button via script.
public Button button;
public GameObject objectToDeactivate;
void Start() {
button.onClick.AddListener(delegate { DeactiveObject(objectToDeactivate); });
}
void DeactiveObject(GameObject go) {
myObject.SetActive(false);
}
Your answer
Follow this Question
Related Questions
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
How Do I Enable a GameObject upon Button Press? 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer