Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 EdenAlon · Dec 02, 2020 at 03:58 PM · arrayaudiolistsoundaudioclip

Make the audio manager select from an array of sounds

I have two scripts that handle sounds. One is an AudioManager that should play the sounds I want, Another is a"Sounds" class which handles each sound for itself.

It works great with one AudioClip, However, I'd like to make the AudioClips in the Sounds class as Arrays(or lists?) and I am having trouble making it work. I get an index out of range.

Here is the Sounds class:

 using System.Collections.Generic;
 using  UnityEngine.Audio;
 using UnityEngine;
 
 
 [System.Serializable]
 public class Sounds
 {
     public string name;
     public AudioClip[]  audioClip;
 
     [Range(0f, 1f)]
     public float volume;
     [Range(0f, 1f)]
     public float pitch;
 
     public bool loop;
 
     public AudioMixerGroup audioMixerGroup;
     
     [HideInInspector]
     public AudioSource audioSource;
     
 
 }

here is the AudioManager Script:

 using System;
 using System.Collections.Generic;
 using UnityEngine.Audio;
 using UnityEngine;
 using Random = System.Random;
 
 public class AudioManagerNew : MonoBehaviour
 {
   public Sounds[] sounds;
   public static AudioManagerNew instance; 
 
   private void Awake()
   {
     DontDestroyOnLoad(gameObject);
     
     if (instance == null)
     {
       instance = this;
     }
     else
     {
       Destroy(gameObject);
       return;
     }
    
     foreach (var s in sounds)
     {
       s.audioSource = gameObject.AddComponent<AudioSource>();
       s.audioSource.clip = s.audioClip[UnityEngine.Random.Range(0, s.audioClip.Length)];
       s.audioSource.volume = s.volume;
       s.audioSource.pitch = s.pitch;
       s.audioSource.outputAudioMixerGroup = s.audioMixerGroup;
       s.audioSource.loop = s.loop;
     }
   }
   
   public void PlaySound(string name)
   {
     Sounds s = Array.Find(sounds, sound => sound.name == name);
      s.audioSource.Play();
      if (s == null)
      {
        Debug.LogWarning("Sound" + name + "not found");
        return;
      }
      
   }
   //How to play sounds from other scripts:
   // FindObjectOfType<AudioManager>().Play("string of clip name");
 }

I'm clearly not writing the array the correct way, how can I make the audio manager recognize the AudioClip array?

Comment
Add comment · Show 1
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 xxmariofer · Dec 02, 2020 at 04:04 PM 0
Share

where are you getting the index out of range? are you sure you have drag and drop all the audio clips into the sound class and all the sounds in themanager class?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rorrors · Dec 02, 2020 at 05:35 PM

Just a easy script to test. Put it on a gameobject1, give up the ammount of tracks you want to play. Then create gameobjects for the soundtracks, add a compoment "audio source". drag you audio track in it. Turn off, play on awake. Then drag those soundtracks, into the gameobject with the script.

   public AudioSource soundtracks;
     public AudioSource[] soundtracksArray;
 
     void Update()
     {
             if (!soundtracks.isPlaying)
             {
                 PlayAudioTracksRandom();
             }
         }
     }
     void PlayAudioTracksRandom()
     {
             if (!soundtracks.isPlaying)
             {
                 soundtracks = soundtracksArray[Random.Range(0, soundtracksArray.Length)];
                 soundtracks.Play();
                 soundtracks.PlayDelayed(44100);
             }
     }



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

199 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 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

Play sound from an array, not random, and only once 3 Answers

How Do you stop audio of another app? 0 Answers

How many audio clip arrays are supported? 1 Answer

Forward and Backward 3D sound? 1 Answer

How to get decibel's value of game's sound? 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