Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

AudioClip[] Array, initialization & audio sources

Discussion in 'Scripting' started by Ab21392, Aug 6, 2014.

  1. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    I am trying to create my own audio manager script that attachs to the camera with 3 audio sources defining the input so I can control each ones volume with NGUI sliders this works just fine and allows me keep volume global settings as below.

    Code (CSharp):
    1.     public float MasterVol = 1;
    2.     public float MusicVol = 1;
    3.     public float SFXVol = 1;
    4.     public float VoiceVol = 1;
    What I have got stuck with is I have 3x AudioClip[] arrays that I wish to drag and drop the files onto to fill it

    How do I initalize the array if I drag and drop the audio files to the array they are set out as follows.

    Code (CSharp):
    1.     public AudioSource Music;
    2.     public AudioSource SFX;
    3.     public AudioSource Voice;
    Code (CSharp):
    1.     public AudioClip[] MClips; // Audio store for all music clips
    2.     public AudioClip[] SClips; // Audio store for all SFX Clips
    3.     public AudioClip[] VClips; // Audio Store for all Voice Clips
    I also need to then call a function for each of the 3 sources and assign the array clips to one of each so I can keep the sources seperate any ideas and insight into this would be great.

    I will post the full script below

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.     public AudioSource Music;
    8.     public AudioSource SFX;
    9.     public AudioSource Voice;
    10.  
    11.     public float MasterVol = 1;
    12.     public float MusicVol = 1;
    13.     public float SFXVol = 1;
    14.     public float VoiceVol = 1;
    15.  
    16.     public int MIndex = 0;
    17.     public int SIndex = 0;
    18.     public int VIndex = 0;
    19.  
    20.     public AudioClip[] MClips;
    21.     public AudioClip[] SClips;
    22.     public AudioClip[] VClips;
    23.  
    24.     private AudioManager instance;
    25.  
    26.     void awake()
    27.     {
    28.         if (instance != null && instance != this)
    29.         {
    30.             Destroy (this.gameObject);
    31.             return;
    32.         }
    33.  
    34.         else
    35.         {
    36.             instance = this;
    37.         }
    38.         //DontDestroyOnLoad (this.gameObject);
    39.     }
    40.  
    41.     public AudioManager GetInstance()
    42.     {
    43.         return instance;
    44.     }
    45.  
    46.     void start()
    47.     {
    48.  
    49.     }
    50.  
    51.     public void MasterVolumeChange (float MasterVolume)
    52.     {
    53.         MasterVol = MasterVolume;
    54.         AudioListener.volume = MasterVol;
    55.     }
    56.    
    57.     public void MusicVolumeChange (float MusicVolume)
    58.     {
    59.         if (MasterVol <= 1)
    60.         {
    61.             MusicVol = MusicVolume;
    62.             Music.ignoreListenerVolume = false;
    63.             Music.volume = MusicVol;
    64.         }
    65.  
    66.         else
    67.         {
    68.             MusicVol = MusicVolume;
    69.             Music.ignoreListenerVolume = true;
    70.             Music.volume = MusicVol;
    71.         }
    72.     }
    73.  
    74.     public void SFXVolumeChange (float SFXVolume)
    75.     {
    76.         if (MasterVol <= 1)
    77.         {
    78.             SFXVol = SFXVolume;
    79.             SFX.ignoreListenerVolume = false;
    80.             SFX.volume = SFXVol;
    81.         }
    82.  
    83.         else
    84.         {
    85.             SFXVol = SFXVolume;
    86.             SFX.ignoreListenerVolume = true;
    87.             SFX.volume = SFXVol;
    88.         }
    89.     }
    90.  
    91.     public void VoiceVolumeChange (float VoiceVolume)
    92.     {
    93.         if (MasterVol <= 1)
    94.         {
    95.             VoiceVol = VoiceVolume;
    96.             Voice.ignoreListenerVolume = false;
    97.             Voice.volume = VoiceVol;
    98.  
    99.         }
    100.  
    101.         else
    102.         {
    103.             VoiceVol = VoiceVolume;
    104.             Voice.ignoreListenerVolume = true;
    105.             Voice.volume = VoiceVol;
    106.         }
    107.     }
    108. }
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    You don't have to, the serialization system will do it for you. During a Unity Object's instantiation any serialized data provided for that object is used to populate its public or serializable fields. This means that if you mark something as public or [SerializeField] and give it data in the Inspector, your code can access that data as if you'd initialized it yourself.
     
  3. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    Thank you for the tip, I have another question, I have created a function, is it possible to send a parameter or more so how I can edit it so it will accept a parameter for instance I have 5 audio tracks in my array I want this function to be universal to play all music tracks but I only want to play number 3 from an external script? I hope that makes sense or I am clear enough

    Code (CSharp):
    1.     public void PlayMusic()
    2.     {
    3.         if (Music != null && MClips !=null)
    4.         {
    5.             if (MasterVol <= 1)
    6.             {
    7.                 Music.clip = MClips [MIndex];
    8.                 Music.ignoreListenerVolume = false;
    9.                 Music.PlayOneShot(MClips[MIndex], MusicVol);
    10.             }
    11.      
    12.  
    13.             else
    14.             {
    15.                 Music.clip = MClips [MIndex];
    16.                 Music.ignoreListenerVolume = true;
    17.                 Music.PlayOneShot(MClips[MIndex], MusicVol);
    18.             }
    19.         }
    20.     }
     
  4. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
    You could do something like this (if I've understood you correctly):

    Code (CSharp):
    1.  
    2.  
    3. public void PlayMusic()
    4. {
    5.     PlayMusic(MIndex);
    6. }
    7.  
    8. public void PlayMusic(int useIndex)
    9. {
    10.     if (Music != null && MClips !=null)
    11.     {
    12.         if (MasterVol <= 1)
    13.         {
    14.             Music.clip = MClips [useIndex];
    15.             Music.ignoreListenerVolume = false;
    16.             Music.PlayOneShot(MClips[useIndex], MusicVol);
    17.         }
    18.         else
    19.         {
    20.             Music.clip = MClips [useIndex];
    21.             Music.ignoreListenerVolume = true;
    22.             Music.PlayOneShot(MClips[useIndex], MusicVol);
    23.         }
    24.     }
    25. }
    26.  
    This will default to using MIndex when you call it without any argument, but playing with the index given if you give one.
     
  5. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    This seems like it what I require I shall test it out and will post back if any problems as I am not at my usual pc with all the source code on :)
     
  6. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    I have either misunderstood something or I am doing things wrong but to double check I am still unsure how I am to select a index number for int useIndex I have tried to pass a variable through it with sendmessage for testing and no music is played at all

    I have worked with lists before but I seem unable still to be able to pass the int variable over so only that one audio clip is played through the function, it does work however I do not use any variable and just PlayMusic tied to a button and using the first function you posted it will play only the first music clip every time it's pressed, if I use the second int which is the one I wish to parse the index number on is the one that currently doesn't work for me as I am unsure how to parse the index over from another script it seems.

    If I use the PlayMusic on a button without the second field with an int in it I get an error which I had expected just very confused right now with what I am doing wrong.

    clarity recap, I wish to use the PlayMusic(int useIndex) but unsure how I am to send the int useIndex for example track 3 from another script / game object so the Main Musicplay function will play it from the drag & dropped on Audio clip array, or how to access the MIndex from another script if I am doing it correct

    Code (CSharp):
    1. Target.SendMessage(PlayMusic, which int goes here??, SendMessageOptions.Don'tRequireReceiver);
     
    Last edited: Aug 6, 2014
  7. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
    Code (CSharp):
    1.  
    2. AudioManager.GetInstance().PlayMusic( 2 );
    3.  
     
  8. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    Thank you for the help, I also got it to work by using the code below and then use SomeScript.PlayMusic(1); etc which is a huge step for me to work out how to get this audio system coded to how I would like it :)

    Edit And whoops I needed to make the Audiomanager instance static but all working now :)

    Code (CSharp):
    1. public AudioManager SomeScript;
     
    Last edited: Aug 7, 2014
  9. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
    Glad I could help, good to see you got it working :)