[Solved] Array is empty after Scene change..
Hello everyone,
I'm having trouble with an Array and I hope that somebody has an idea.
I've written an AudioControl Script to load Music into an Array.
public AudioClip[] mClips;
mClips = Resources.LoadAll<AudioClip> ("Audio/Music");
This works fine. In my MainMenu Scene and MainMenu.cs Script, I can play the music by script. To check the array I made a Debug.Log.
foreach (AudioClip getTheClips in audioControl.mClips)
Debug.Log ("MainMenu: " + getTheClips);
The result in my console is fine:
MainMenu: Soft1 (UnityEngine.AudioClip)
MainMenu: Stage1 (UnityEngine.AudioClip)
When I start the Game and switch the Scenes from MainMenu to Level_00 by SceneManger.LoadLevel the Array is used in my HealthController.cs
In the HealthController.cs I did the following to check the Array:
foreach(AudioClip theClips in audioControl.mClips)
Debug.Log ("HealthController: " + theClips);
The result in console is just: HealthController:
No Error Message or Warning.
If I try:
public AudioClip levelMusic;
levelmusic = audioControl.mClips [1];
Debug.Log = (levelmusic);
It returns just "Null" in Console.
public AudioControl audioControl;
//In Startfunction on both Scripts
audioControl = GetComponent<AudioControl> ();
Any help is appreciated.
Are you sure you have [DontDestroyOnLoad] as an attribute for your class?
I don't have [DontDestroyOnLoad] anywhere.
If I look at the Inspector after the Scene changed, the Array has been loaded. All clips are listed there, but I can't access them and it says still null.
Are you expecting that array to be saved between scenes and sessions? If so, can you tell us what class it is a member of? This is important because certain classes have automatic serialization built in, like for public members of $$anonymous$$onoBehavior's, or ScriptableObjects. If you did NOT derive from a class that does this for you, you may need to implement the ISerializationCallbackReciever Interface for your custom class. (https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html)
If you ARE using a $$anonymous$$onobehavior, it's important to use the DontDestroyOnLoadfunction Firedan suggested. This will prevent the object from re-initializing on scene load, with the saved data mentioned above.
Other alternatives: 1) reinit the array on scene load. 2) after initializing the array, save the object holding it as a pre-fab, and load the pre-fab at scene load.
Answer by Hethu · Aug 10, 2016 at 01:32 PM
Oh my, I tried so much.. Even with Script Execution Order.
I have no idea why, but changing this did it..
using UnityEngine;
using System.Collections;
public class AudioControl : MonoBehaviour {
public AudioClip[] mClips;
void Awake () // it was .. void Start()
{
mClips = Resources.LoadAll<AudioClip> ("Audio/Music");
}
}
Sure, there is way more code inside AudioControl.cs (just used the important code) but the Array isn't empty in the Healthcontroller.cs anymore and Music works fine now after Scene change.
So, for now it's Solved and hope it will work after the Build, but thank you both anyway for your ideas. That gave me alot of thoughts for the future to keep in mind.
The Class and your initializing made me think , so i changed it from Start() to Awake(); I'm still new to Unity and C#, sorry for any inconvenience.
This should make it clear why that helped: https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
No inconvenience:) There are so many nitty-gritty details, it's hard to account for 'em all, even when NOT new to unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220612090508im_/https://answers.unity.com/themes/thub/images/avi.jpg)