- Home /
Enable UI Button from Prefab
Hey guys, I have the problem that I cannot set my Button vars from my script to the appropriate UI buttons.
What I have going on is that when my "Player" prefab dies, it destroys the game object and makes two buttons appear by enabling them. In my code I am using the UnityEngine.UI and the vars for the buttons are of type button, however when I go into the editor to drag and drop the buttons from the hierarchy into the 'None (Button)' slots under the script, it does not allow me to do so.
But it does allow me to drag and drop in the UI buttons correctly if I drag an instance of the player prefab into my scene and then try and do it. (But it does not apply this back onto the prefab itself if I hit apply.)
Can anyone tell my why this is happening/ how to fix it? Thanks. Here's the code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class CollisionDetection : MonoBehaviour {
public bool IsGameOver;
public int Health;
public Button resetButton;
public Button menuButton;
public GameObject BloodParticles;
void Awake (){
IsGameOver = false;
Health = 10;
}
void OnCollisionEnter2D (Collision2D col)
{
if(col.gameObject.name == "Enemy 1 - Buzzsaw(Clone)")
{
Health -= 1;
Debug.Log ("Health is " + Health);
}
if(col.gameObject.name == "Enemy 3 - Drone(Clone)")
{
Health -= 1;
Debug.Log ("Health is " + Health);
}
if(col.gameObject.name == "BulletPrefab(Clone)")
{
Health -= 1;
Debug.Log ("Health is " + Health);
}
if (Health == 0){
Debug.Log ("GAME OVER");
IsGameOver = true;
resetButton.gameObject.SetActive (true);
menuButton.gameObject.SetActive (true);
Destroy (gameObject);
GameObject Clone;
Clone = (Instantiate (BloodParticles, transform.position,transform.rotation))as GameObject;
}
}
}
What buttons are you attempting to drag into the UI Button slots??
Answer by _dns_ · Jul 28, 2015 at 02:37 PM
Hi, if I understand well, what you are trying to do is referencing an instance in a scene (buttons) from a prefab (player). This is not possible as the prefab "lives" on the disk and doesn't know anything about any scene or instances in it, this is a different environment. This is not easy to explain, imagine that in a scene, each object has an ID that is different each time the scene is loaded. Then, a prefab cannot know what ID an object has in advance. In the contrary, an object in a scene can know the ID of a prefab as it's on the disk and then has a "fixed" filename to reference it (I simplify here, you can read more about this in Unity documentation/tutorial)
So, to do what you want to do: When the player dies, it has to find the UI object to be able to reference them. You can use GameObject.Find to find the UI object by it's name, then enable them. The trick is that gameobject.find does not find disabled/inactive objects.
So what you could do is let the buttons enabled in the scene, then, during the player's Start(), find the buttons by their name, assign them to some private fields of your player class (like a private GameObject MyFirstButton;) and then disable the buttons to hide them before the game begins. Then, on player death, you just have to use MyFirstButton.SetActive to enable them.