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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

[iOS] Streaming audio with loop 1 Answer

Increase the volume of sound source past limit? 2 Answers

Using an Audio Listener on an Audio Source 1 Answer

Control Mono/Stereo via Audio Mixer? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges