Hello everybody,
When I instantiate a prefab with the follow script, GetComponentInChildren() fails in both functions:
void Awake()
{
Component c = GetComponentInChildren(typeof(Renderer));
if (!c)
Debug.LogError("Awake: Failed");
else
Debug.Log("Awake: Succeeded");
}
void OnEnable()
{
Component c = GetComponentInChildren(typeof(Renderer));
if (!c)
Debug.LogError("OnEnable: Failed");
else
Debug.Log("OnEnable: Succeeded");
}
If I remove OnEnable(), Awake() will succeed; If I remove Awake(), OnEnable() still fails.
From my understanding, both functions should never fail.
Is it a bug or I’ve missed something? Thank you for attention.
200293–7374–$onenable_653.zip (1.06 MB)
Erm… sorry to spoil the fun, but the call in Awake and the other one in OnEnable both work for me in the project you’ve posted! Did you start off with this script in another project where this didn’t work or does this same project fail when you try to use it?
Yes, I run the same project. That’s strange if you can run it corectly…
Actually, I now see the mistake I was making… you seem to be using Object.Instantiate from an instance of the prefab in the scene. Since you’re instantiating in the Awake method, this can cause problems (it is dependent on the order in which objects are awoken and this order is arbitrary). Rather than drag an instance of the prefab from the scene onto the script variable, drag the prefab object itself from the Project panel. Alternatively, you can use Resources.Load to create an instance if it is more convenient.
The prefab is instantiated in StartTest.Start(), which is the similar mechanism as instantiating Lerpz prefab in the 3D tutorial. So I find that’s strange to have such error.