Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Music Manager

Discussion in 'Getting Started' started by joytdecastro, Mar 1, 2015.

  1. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    i got this code, but there is an error, it says that
    IndexOutOfRangeException: Array index is out of range.
    AudioManager.Start () (at Assets/AudioManager.cs:34)
    how can i fix it??

    here's the code..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum SoundEffects { ObstacleCollisionSoundEffect, CoinSoundEffect, PowerUpSoundEffect, GameOverSoundEffect, GUITapSoundEffect }
    5. public class AudioManager : MonoBehaviour {
    6.    
    7.     static public AudioManager instance;
    8.    
    9.     public AudioClip backgroundMusic;
    10.     public AudioClip obstacleCollision;
    11.     public AudioClip coinCollection;
    12.     public AudioClip powerUpCollection;
    13.     public AudioClip gameOver;
    14.     public AudioClip guiTap;
    15.    
    16.     public float backgroundMusicVolume;
    17.     public float soundEffectsVolume;
    18.    
    19.     private AudioSource backgroundAudio;
    20.     // use multiple sound effects audo sources so more than one sound effect can be played at the same time
    21.     private AudioSource[] soundEffectsAudio;
    22.     private int nextSoundEffectsAudioIndex = 0;
    23.    
    24.     public void Awake()
    25.     {
    26.         instance = this;
    27.     }
    28.    
    29.     public void Start()
    30.     {
    31.         AudioSource[] sources = Camera.main.GetComponents<AudioSource>();
    32.         //backgroundAudio = sources[0];
    33.         soundEffectsAudio = new AudioSource[2];
    34.         soundEffectsAudio[0] = sources[1];
    35.         soundEffectsAudio[1] = sources[2];
    36.        
    37.         backgroundAudio.clip = backgroundMusic;
    38.         backgroundAudio.loop = true;
    39.         backgroundAudio.volume = Mathf.Clamp01(backgroundMusicVolume);
    40.        
    41.         soundEffectsAudio[0].volume = Mathf.Clamp01(soundEffectsVolume);
    42.         soundEffectsAudio[1].volume = Mathf.Clamp01(soundEffectsVolume);
    43.     }
    44.    
    45.     public void playBackgroundMusic(bool play)
    46.     {
    47.         if (play) {
    48.             backgroundAudio.Play();
    49.         } else {
    50.             backgroundAudio.Pause();
    51.         }
    52.     }
    53.    
    54.     public void playSoundEffect(SoundEffects soundEffect)
    55.     {
    56.         AudioClip clip = null;
    57.         float pitch = 1;
    58.         switch (soundEffect) {
    59.         case SoundEffects.ObstacleCollisionSoundEffect:
    60.             clip = obstacleCollision;
    61.             break;
    62.            
    63.         case SoundEffects.CoinSoundEffect:
    64.             clip = coinCollection;
    65.             pitch = 1.5f;
    66.             break;
    67.            
    68.         case SoundEffects.PowerUpSoundEffect:
    69.             clip = powerUpCollection;
    70.             break;
    71.            
    72.         case SoundEffects.GameOverSoundEffect:
    73.             clip = gameOver;
    74.             break;
    75.            
    76.         case SoundEffects.GUITapSoundEffect:
    77.             clip = guiTap;
    78.             break;
    79.         }
    80.        
    81.         soundEffectsAudio[nextSoundEffectsAudioIndex].pitch = pitch;
    82.         soundEffectsAudio[nextSoundEffectsAudioIndex].clip = clip;
    83.         soundEffectsAudio[nextSoundEffectsAudioIndex].Play();
    84.         nextSoundEffectsAudioIndex = (nextSoundEffectsAudioIndex + 1) % soundEffectsAudio.Length;
    85.     }
    86. }
    87.  
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    The Camera referenced by Camera.main does not have any AudioSource components on it.
     
  3. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    so should i put the script on the camera??
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    No.

    According to the code you are using, the main camera needs a total of 3 AudioSource components attached to it.