Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SubatomicHero · Feb 21, 2013 at 06:28 PM · audioarraysaudio sourceaudio clip

Array of AudioClips not playing, please help

Hi all,

I have an array of 4 sounds added as an audioclip array:

 public AudioClip[] sounds;

and I initialise it in Start() like so:

 for (int i=0; i<4; i++)
         {
             sounds = new AudioClip[i];    
         }

Later on in my code I want to play a random sound from that bank. My object has an audiosource attached. The code I am using is:

 audio.clip = sounds[Random.Range(0, sounds.Length)];
 audio.Play();

The sounds are assigned in the inspector yet when my scene runs none of the sounds play.

What am I doing wrong?

Thanks in advance,

Martin

Comment
Add comment · Show 2
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 SubatomicHero · Feb 21, 2013 at 08:15 PM 1
Share

Yeah I removed the initialization and it worked first time. I was under the understanding that you had to initialize each array position before you could use it :D

avatar image robertbu · Feb 21, 2013 at 08:20 PM 0
Share

FYI, this should have been a comment ins$$anonymous$$d of an answer.

In general you do have to initialize both the array as a whole and the individual elements. The way you went about the initialization it is wrong for general initialization. But you have a public variable and you set of the array size in the inspector and drag and drop items into the array into the inspector, Unity does this initialization for you.

3 Replies

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

Answer by robertbu · Feb 21, 2013 at 06:54 PM

Your problem is here:

 for (int i=0; i<4; i++)
        {
          sounds = new AudioClip[i];   
        }

Just kill this initialization. This is recreating the array four times, each time a little longer, but in the process it blows away the original clips you assigned in the inspector.

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
avatar image
0
Wiki

Answer by wholebitmedia · Feb 21, 2013 at 07:19 PM

You might try a case statement for this idea. I remember having a lot of issues with audio when I was learning it. In any case, if you are using C# you might need to indicate the desired audio inside the parenthesis after your Play command.

audio.Play(walk); or audio.PlayOneShot(land);

anyone else feel free to "chime" it!?

Tha5 was just a little programming humor! :)

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
avatar image
0

Answer by Unity-Devloper · Jun 17, 2020 at 06:27 AM

     using UnityEngine;
     using System.Collections;
     using UnityEngine.UI;
     
         /// <summary>
         /// Class in charge to play sound in the game
         /// </summary>
         public class SoundManager : MonoBehaviour 
         {
             public AudioSource GamePlaySound;
             /// <summary>
             /// Reference to the jump sound
             /// </summary>
             public AudioClip[] soundClips;
     
             /// <summary>
             /// Reference to the audiosource who will play the sounds
             /// </summary>
             AudioSource audioSource;
     
            void Awake()
             {
                 audioSource = GetComponent<AudioSource> ();
             }
     
            public void PlaySoundClip()
             {
                 audioSource.PlayOneShot(soundClips[Random.Range(0,soundClips.Length)]);
                 ;
             }
         }
 
 
 




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

11 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

Related Questions

Loading MP3's into an Audio Array and assigning them to members of a GameObject Array 1 Answer

Audio on object plays low in unity 5 1 Answer

if !audio.isPlaying for any object, not just this one. 1 Answer

Problem with audio cue playing in VR then the audio staying headlocked with ear it played from, when it should stay at the audio object location. 0 Answers

Background music doesn't start on new scene load. 0 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