- Home /
Object reference becomes null between Awake and Start after scene load
I have an odd problem. I'm using the same pattern throughout a bunch of components - in Awake() I find a reference to an object in the scene, and then in Start() I call a function on that object.
For one of my components (attached to my player object), the reference to the object becomes null between the Awake() and Start() calls. The variable is private and there's no function within the class changing its value, so short of Unity messing with things behind the scenes or actual memory corruption I'm not sure how this is possible. Anyone have any ideas?
The object in question (the saver) is DontDestroyOnLoad, and this happens only after loading a new scene.
private SaveManager saver;
void Awake()
{
GameObject SManager = GameObject.Find("SaveManager");
if (SManager)
{
saver = SManager.GetComponent<SaveManager>();
if (!saver)
{
Debug.LogError("No save manager in the scene!");
}
}
//"saver" is a valid reference here.
}
void Start()
{
//"saver" is null here
//If I Find() it again, it's a valid reference.
}
Answer by Kryptos · Aug 01, 2012 at 10:16 AM
Is this script also on a DontDestroyOnLoad object? If it is the case, Awake is not called again. And the solution would be to do the "Find" in the OnLevelWasLoaded method.
Nope. Both Awake() and Start() are called, in the proper order (I use Debug.Log() to output the value of saver in each).
Since I use the same kind of code in all my project, I don't understand it. Are you sure that nothing else can modify this value?
None of my code modifies it. It's possible something in Unity does, though. Or GC, for some reason? I have other objects using this same pattern, and none exhibit the issue, so it's something about this component/object.
Answer by Dzxyan · Oct 14, 2016 at 11:18 AM
Hi I know this is old topic,but I also have this problem, do you have solution for that?