- Home /
how to activate a object with button?
So I use a button assest for android an so my button looks like this and I want to activate a object thats deactivated with "gameObject.SetActive (false);.
using UnityEngine; using System.Collections;
public class spawnParachute : MonoBehaviour {
//cube I want to spawn
public GameObject Cube;
//button to use for going to the game
public EasyButton parachute;
//this is the buttons name after "string"
void On_ButtonUp (string parachute)
{
// thats the button and string
if (parachute == "parachute")
{
//This is the only part worth looking at
GameObject.Find("Cube").SetActive (true);
}
}
}
I get the error "object refrence not set to instance of object"
Answer by ThePunisher · Apr 04, 2014 at 08:20 PM
GameObject.Find will only retrieve objects that are enabled.
You can find this information in the unity scripting reference: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
"This function only returns active gameobjects."
What you will want to do is keep a reference to the object before you disable it. Then use that reference to re-enable it.
The code would look something like this:
public class Temp : MonoBehaviour
{
private GameObject m_cube;
// Use this for initialization
void Start ()
{
m_cube = GameObject.Find("Cube");
m_cube.SetActiveRecursively(false);
}
void EnableObject()
{
m_cube.SetActiveRecursively(true);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613141939im_/https://answers.unity.com/themes/thub/images/avi.jpg)