- Home /
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];) 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.
Absolute shot in the dark but you might be setting references to the skills and not the actual skills.
You might be setting the array equal to the object in the scene and then when the scene is destroyed then the reference is destroyed with it so your array loses its reference to that object.
Thats what my guess would be. That you have something like SkillsArray[0] = SkillButton.SkillScript or something like that. So when the skillbutton gets destroyed then you lose the reference.
Unfortunately i am not sure how to solve it but that would be the problem I think
Maybe you're right. I just simply want to save a copy of these three scripts from these three buttons. It seems like it should be something so simple, but I just don't know how.
Answer by andzq · Apr 02, 2021 at 11:47 AM
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.
I've converted your comment into an answer as you are spot on, so I don't have to repeat everything once more ^^.
Thank you, andzq! That helps me to understand exactly how I misunderstood my process and where it went wrong.
Your answer
Follow this Question
Related Questions
(JS) Calling a member function of an object in an array 2 Answers
create objects by script and fill them into array 0 Answers
Activate objects in array based on value 3 Answers
Keeping track of array objects 2 Answers
How can I Instantiate an Object from a Randomly Selected Index in an Array of Game Objects? 1 Answer