- Home /
Load and switching level problem
Greetings,
currently i have two levels:
Level 1 is the main menu,with 3 images called "glock","deagle" and "uzi" inside a Canvas,each one with a child image called "equip_button".
Level 2 is a map with enemies and has 3 GameObjects also called "glock","deagle" and "uzi",each one with a child object called "flash".
So my objective is when i change from Level 2 to Level 1 the "equip_button" color of the images changes depending of the current weapon:
Script on the Level 1
On Start():
GameObject[] weapons = GameObject.FindGameObjectsWithTag ("weapon");
In the Update():
foreach (GameObject child in weapons) {
if(current_weapon==child.transform.name)
child.transform.FindChild ("equip_button").GetComponent<Image> ().color = new Color (1, 1, 1, 1);
}
but instead,i get an error of "Object reference not set to an instance of an object" in the line where i do the FindChild("equip_button").
Debugging and using another foreach i saw that the 3 objects saved on the "weapons" var are the objects of the Level 1!!!(becouse they had the "flash" child instead the "equip_button").So my problem is that the Level 1 script is getting the objects of the Level 2,and that's not possible becouse using the Application.LoadLevel() all GameObjects of the Level 2 should be destroyed...
Take some extra and helpfull info:
I'm not using the DontDestroyOnLoad method in any object.
I put on the start() of the Level 1 script a Caching.CleanCache () but didn't work.
Closing Unity and opening again solves the problem,so i think that this is a memory or cache problem.
The "glock" "deagle" and "uzi" objects of both levels have the same tag("weapon"),but i'm sure that this is not the problem becouse before getting Unity 5 and using Mecanim all worked perfectly!
Thanks for your attention and sorry for my bad english.
I'd like to help but have no idea what to ask for. Can you share a unity package with just the scenes and the buttons reproducing the bug? $$anonymous$$aybe someone sees something else causing this weird behaviour.
The 2 scenes has a lot of objects and prefabs,so the package size would be a nightmare to upload and download.Anyway this happens randomly when i change of scene and restarting the unity editor solves the problem,so i think that it is bug of the editor or a memory/cache problem.You know any command to clear ALL?(specially about tags)
Caching.CleanCache (); didn't work and something is cleaning when i restart Unity to make it work.
Your answer
![](https://koobas.hobune.stream/wayback/20220612035714im_/https://answers.unity.com/themes/thub/images/avi.jpg)