Cant find component after scene load but it exists ! HELP!
Hi there, i am facing a huge problem.
I am trying to push scripts reference into a list of the script type on start.
I am using a global Singleton which call the start of all other objects in my prefered order.
After loading the scene from the menu, i am trying to push some gameobject scripts but it gives me "Object reference not set to an instance of an object" when the script & object do exist.
Why do i know he exist?
Just before adding the script to the list, i did a Debug.log of one of the script property and it does return the property correctly. But when i add it to the list, it says the error i just said.
This is very weird and very frustrating. Anyone has a solution?
void Start()
{
Debug.Log("Game start");
Transform tr = GameObject.Find("_SceneObjects").transform.FindChild("Cubes");
for (int i = 0; i < 8; i++)
{
Debug.Log(tr.GetChild(i).GetChild(0).gameObject.GetComponent<FlipBehaviour>().m_order); //WORKS
m_cubes.Add(tr.GetChild(i).GetChild(0).gameObject.GetComponent<FlipBehaviour>());//OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT
}
//-------------- START------------------
ClassicModeManager.Instance._Start();
ResourcesManager.Instance._Start();
UIManager.Instance._Start();
foreach (FlipBehaviour cube in m_cubes)
{
cube._Start();
}
ClassicModeManager.Instance.NextLevel();
Debug.Log("Ranking : " + ResourcesManager.Instance.Sprites.BronzeRanking.name);
}
EDIT: Im gonna leave this here in case my solution can help others.
For a reason i dont know (but which makes sense), you have to use the "new" key for the List when you load the scene from another scene.
If you test the loaded scene directly, you dont need the "new" key. You can simply create a new list like this:
List list;
but if you load the scene, you have to create a new instance of the list
List list = new List();
and now it works.
I was right, the script was found and exist, but the list is equal to null. So the "Object reference = null" error was for the list and not the component itself.
Answered my own question ahah !
Hope it will help others
Damenz