- Home /
Array has length 0, but I can see its' elements in the inspector
I have a volume slider that changes the volume of each audio source in an array. The problem is that the clips array has length 0 in the debug log, but in the inspector I see it contains the one audio source I have on the object. Trying to get the audio source through GetComponent tells me there's no audio source attached.
I can hear the music playing.
public AudioSource[] clips;
void Start () {
clips = GetComponents<AudioSource>();
}
public void setMusicVolume(float volume)
{
Debug.Log(clips.Length);
foreach (AudioSource clip in clips)
{
clip.volume = volume;
Debug.Log("set volume: " + volume);
}
PlayerPrefs.SetFloat("MusicVolume", volume);
}
my Slider is set to setMusicVolume. Changing the slider prints 0, but nothing else.
On start, another script checks player prefs for the last set volume and it works fine, logging "1" and whatever the volume is.
if (PlayerPrefs.HasKey("MusicVolume")){
float volume = PlayerPrefs.GetFloat("MusicVolume");
DC.setMusicVolume(volume);
MusicSlider.GetComponent<Slider>().value = volume;
}
Answer by Bunny83 · Jan 18, 2019 at 02:26 AM
I suspect that you either may not reference the correct object in your other script. So your "DC" variable may point to the wrong object (maybe a prefab in the project instead of the actual object in the scene). Or you may call your method "setMusicVolume" too early before the scripts Start method has been called.
Finally you may have deactivated the gameobject so Start is never called.
You should use better debug log statements like:
Debug.Log("Number of AudioSources: " + clips.Length + " on gameobject " + gameObject.name, gameObject);
This will print a message that can be uniquely be identified in the console and also prints the objects name. Furthermore we pass "gameObject" as second (context) parameter. Now when you click on the debug log message in the console, the editor will highlight / ping the object on which this code was executed. If a prefab is highlighted you have setup your references wrong.
You were right about the references. I ended up just putting everything into one script, which fixed the problem.
public class volumeControl : $$anonymous$$onoBehaviour
{
public AudioSource[] clips;
// Use this for initialization
void Start()
{
clips = GetComponents<AudioSource>();
if (PlayerPrefs.Has$$anonymous$$ey("$$anonymous$$usicVolume"))
{
float volume = PlayerPrefs.GetFloat("$$anonymous$$usicVolume");
set$$anonymous$$usicVolume(volume);
//$$anonymous$$usicSlider.GetComponent<Slider>().value = volume;
}
}
public void set$$anonymous$$usicVolume(float volume)
{
Debug.Log(clips.Length);
foreach (AudioSource clip in clips)
{
clip.volume = volume;
Debug.Log("set volume: " + volume);
}
PlayerPrefs.SetFloat("$$anonymous$$usicVolume", volume);
}
}
Your answer
Follow this Question
Related Questions
How to make a custom type's contents modifiable in an array by inspector? 1 Answer
How do I make a public array of arrays appear in the inspector? 1 Answer
Multidimentional Array for Inventory style system in inspector. 1 Answer
Trying to be more efficient. Advice please... 1 Answer
Why does new GUIStyle() give "object reference not set to an instance of an object" error? 1 Answer