I have a really weird problem.
When I use GameObject.Find(“Health Bar”) it can’t find the Health Bar.
But when I use GameObject.FindGameObjectWithTag(“Health Bar”) its finds it.
It’s the same for the hunger bar
Here’s the code that doesn’t work:
using UnityEngine;
using System.Collections;
public class CheckforUI : MonoBehaviour {
void Start () {
if(GameObject.Find("Health Bar") == false){
Debug.Log("Health Bar Spawned");
Instantiate(Resources.Load("Health Bar"));
}
else
{
Debug.Log("Health Bar Found");
}
if(GameObject.Find("Hunger Bar") == false){
Debug.Log("Hunger Bar Spawned");
Instantiate(Resources.Load("Hunger Bar"));
}
else
{
Debug.Log("Hunger Bar Found");
}
}
}
And here is the one that works:
using UnityEngine;
using System.Collections;
public class CheckforUI : MonoBehaviour {
void Start () {
if(GameObject.FindGameObjectWithTag("Health Bar") == false){
Debug.Log("Health Bar Spawned");
Instantiate(Resources.Load("Health Bar"));
}
else
{
Debug.Log("Health Bar Found");
}
if(GameObject.FindGameObjectWithTag("Hunger Bar") == false){
Debug.Log("Hunger Bar Spawned");
Instantiate(Resources.Load("Hunger Bar"));
}
else
{
Debug.Log("Hunger Bar Found");
}
}
}
Why is this happening??
Cheers