Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Hethu · Aug 09, 2016 at 09:09 PM · c#arraynullaudioclips array

[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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Firedan1176 · Aug 10, 2016 at 01:51 AM 0
Share

Are you sure you have [DontDestroyOnLoad] as an attribute for your class?

avatar image Hethu Firedan1176 · Aug 10, 2016 at 11:52 AM 0
Share

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.

avatar image Glurth · Aug 10, 2016 at 12:55 PM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Aug 10, 2016 at 02:41 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

217 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bool[] Array index is out of range 0 Answers

how to start counting from the last index the script stop on? 2 Answers

Why are my arrays in arrays null (converting from JSON)? 1 Answer

Getting collision from gameobjects in an array, and changing properties of other gameobjects from that array 0 Answers

Getting variables that from an array of scripts 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges