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 /
avatar image
0
Question by kcfos · Jan 18, 2019 at 02:00 AM · arrayinspectorslider

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;
         }

alt text

help.png (67.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

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 kcfos · Jan 18, 2019 at 03:02 AM 0
Share

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

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

118 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

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


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