So when I instantiate a prefab, it looks for a gameObject with the tag of “Inventory”. However, it doesn’t work properly. What DOES work is when I instantiate it, pause UnityEditor and then un pause the game and then it works. Here is my script.
public class FireTurnOn : MonoBehaviour {
public GameObject Canvas;
void Awake()
{
foreach (RectTransform go in GameObject.FindObjectsOfType<RectTransform>())
{
//print(go.tag);
if (go.tag == "Inventory")
{
Canvas = go.gameObject;
}
}
}
void Update ()
{
if (Canvas == null)
{
Awake();
}
}
void Interact ()
{
if (Canvas == null)
{
foreach (RectTransform go in GameObject.FindObjectsOfType<RectTransform>())
{
//print(go.tag);
if (go.tag == "Inventory")
{
print("Found the canvas");
Canvas = go.gameObject;
}
}
}
bool hasWood = Canvas.GetComponent<Inventory>().FindWoodSlot() != null;
if (hasWood)
{
GameObject woodSlot = Canvas.GetComponent<Inventory>().FindWoodSlot();
woodSlot.GetComponent<UnityEngine.UI.Image>().sprite = Canvas.GetComponent<Inventory>().Empty;
transform.GetChild(0).gameObject.SetActive(true);
transform.GetChild(0).gameObject.GetComponent<FireDestroyer>().Start();
}
}
}