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 abragon · Aug 02, 2017 at 06:32 PM · scripting problemaudio

How to loop through a list of sounds if(bool)

UPDATE: I fixed the error. audioSource = GetComponent<AudioSource>() is called in Start() but I Start() is never executed I guess. The following works:

  public void PlaySound()
      {
          audioSource = GetComponent<AudioSource>()
          PlayList();
      }

QUESTION: I have the script below attached to gameobject with a audiosource. When a condition is true, I call the function PlaySound() through another script (and when false I call StopSound()). But this isn't working. I get the error:

 NullReferenceException: Object reference not set to an instance of an object
 PlayEatingSounds.PlayList () (at Assets/Scripts/PlayEatingSounds.cs:30)

At line 30: audioSource.clip = audioClips[Random.Range(0, audioClips.Count)]; The list with audioclips is definitely not empty. I have 0 clues or google results in how to tackle this, please help.

 public class PlayEatingSounds : MonoBehaviour
 {
     public Vector2 pitchRange;
     public List<AudioClip> audioClips = new List<AudioClip>();
 
     private AudioSource audioSource;
     private AudioClip audioClip;
 
     void Start()
     {
         audioSource = GetComponent<AudioSource>();
     }
 
     public void PlaySound()
     {
         PlayList();
     }
 
     public void StopSound()
     {
         CancelInvoke();
     }
 
     void PlayList()
     {
         audioSource.clip = audioClips[Random.Range(0, audioClips.Count)];
         audioSource.pitch = Random.Range(pitchRange.x, pitchRange.y);
         audioSource.Play();
         Invoke("PlayList", audioSource.clip.length);
     }
 }


Comment
Add comment · Show 5
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 ShadyProductions · Aug 02, 2017 at 06:46 PM 0
Share

change public List<AudioClip> audioClips = new List<AudioClip>(); to public AudioClip[] audioClips; and set it in the inspector. and change audioClips.Count to audioClips.Length

avatar image abragon ShadyProductions · Aug 02, 2017 at 07:24 PM 0
Share

Why does it matter if it's a array or List?

avatar image ShadyProductions abragon · Aug 03, 2017 at 07:18 AM 0
Share

It cleaner, less memory + the real problem is probably

 public List<AudioClip> audioClips = new List<AudioClip>();

making a new instance will probably overwrite everything in the editor.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Y00 · Aug 03, 2017 at 11:14 AM

if The list with audioclips is not empty, maybe audioSource is empty? Try to specify it in the inspector instead GetComponent

Comment
Add comment · 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

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

122 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

Related Questions

accessing the previous click object information 0 Answers

audio doesn't play on movement 0 Answers

How can i set audio mixer groups volume to be used with ui slider when 0 is lower and 80 louder ? 1 Answer

Audio Clip Playing every frame 2 Answers

Hello eveyone, i want to make a footstep sound but my code doesn't make the audio work properly 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