objects in Array null when changing scenes

I am using a GameManager script that is attached to an empty gameobject that is not destroyed between scenes. I have two scenes, a world map scene and a battle scene. I use the GameManager to transfer important information and variables from the world map scene to the battle scene.

I have a script called “Skill” that is attached to a few buttons in the world map scene. I have also referenced and declared a “Skill” array (Skill skills = new Skill[3]:wink: in my GameManager script that I use to save the variables and state of my three skill buttons in the world map with hopes that I can fill out each skill button’s “Skill” script in the battle scene with those of the GameManager array. Right before the battle scene is loaded, each skill button saves its script to the skills array in the GameManager. I know that when you load a new scene, all GameObjects in the scene are destroyed upon loading a new scene. I notice that all elements in this array become null when I load the new scene, despite GameManager not being destroyed on scene load.

Why is this? I feel like I am misunderstanding something fundamental. I’d like to make sure whatever changes I am making to my skills in the world map (i.e. improve a skill’s damage) are transferred to the buttons I have in the battle scene. I’d like those buttons to be identical to their other scene’s counterpart.

So you are storing references to the skill scripts in an array of your gamemanager right? If these scripts get destroyed during sceneload the references will also vanish. What’s saved in the gamemanager’s array is just pointers to the skill scripts, not actual copies of the skill scripts and they will point into “null” when the original scripts they were pointing to are destroyed due to the scene unload.

If you want to store the data you gotta take another approach. You could flag the skill scripts as “Don’t destroy on load” just like your gamemanager. Or you could create a little helper class or struct within your gamemanager that can store all the values you would need to “re-create” identical skill scripts when the new scene is loaded. Depending on the needed data, you could also save the skill scripts values to “PlayerPrefs” in order to save them for the next scene load or application start.