- Home /
Disabling a prefab
I am trying to edit a script so that I can disable a button in the canvas when the game starts and reactivate it when the player is destroyed. The script is attached to asteroid prefabs (there aren't any actual asteroids in the scene. The script has a public GameObject variable to store the button. Since the asteroids are all prefabs I can't seem to store the button in the variable through the editor, so I created a new prefab to store in the variable. Now use the SetActive commands it does not work on the button. I tried to use GameObject.Find to store the button object in the variable when the game starts but then an error occurs where the the SetActive line is highlighted and it says the command is not set to an object reference.
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class Asteroid : MonoBehaviour {
public string myString = "hit";
public GameObject background;
void Start () {
background = GameObject.Find ("Button");
background.SetActive (false);
}
void Update () {
transform.Translate (Vector3.back * Time.deltaTime * Random.Range (3,7));
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag("Player")) {
Destroy (GameObject.Find ("Cube"));
background.SetActive (true);
}
if (other.gameObject.CompareTag ("Pick up")) {
Destroy (gameObject);
}
}
}
Your answer