- Home /
Question by
kevinrocks_786 · May 29, 2016 at 03:52 PM ·
c#instantiateunity5startawake
Instantiated Prefab not calling Start() nor Awake()
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();
}
}
}
Comment
Is this the script that spawns the new object, or the one contained inside the spawned object?
Best Answer
Answer by kevinrocks_786 · May 30, 2016 at 02:56 AM
Turns out FindObjectsOfType() only finds objects that are active in the scene and the Inventory wasn't active. Solved.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081850im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
C# property keeps getting overwritten 2 Answers
Class c = new Class() okay in class body? 1 Answer
C# void names 1 Answer
Multiple Cars not working 1 Answer