Search Unity

C# - Random Audioclip - Ressources.Load

Discussion in 'Scripting' started by bigboybifdick, Oct 22, 2014.

  1. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey guys,

    I can't figure out how to associate audioclip to gameobjects randomly.
    Let's just assume we have 2 gameobjects and 2 audioclip.
    The gameobjects have Audio Sources, but no audio in it.

    On runtime i want to be able to give them an audioclip.
    I'm strugling now with the AudioClip[] stuff.

    Here is my code (attached to my camera for testing)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public AudioClip[] audioArray;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.         audioArray =  new AudioClip[]{
    12.             (AudioClip)Resources.Load(son1),
    13.             (AudioClip)Resources.Load(son2),
    14.     };
    15.  
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.    
    21.         audio.clip = audioArray[Random.Range(0, audioArray.Length)];
    22.         audio.Play ();
    23.  
    24.     }
    25. }
    26.  


    No sound playing at all, and no error

    What am i doing wrong ?

    Thanks a lot !
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You are doing it in update
    every frame it is assigning the clip, and then playing from the beginning, giving you one frame of the silence at the start of the track.
     
    Deleted User likes this.
  3. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey,

    Thanks for the answer !
    I changed it but it still doesn't work... No error and no sound... :/

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public AudioClip[] audioArray;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.         audioArray =  new AudioClip[]{
    12.             (AudioClip)Resources.Load("son1"),
    13.             (AudioClip)Resources.Load("son2"),
    14.         };
    15.         audio.clip = audioArray[Random.Range(0, audioArray.Length)];
    16.         audio.Play ();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.      
    22.  
    23.      
    24.     }
    25. }
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Which of the following obvious things have you checked?
    Speakers on and volume up?
    Object has audio source
    Scene has audio Listener
    Clips are in Resources folder
    Clips are not null after loading
     
  5. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey,

    Thanks, not sure what was wrong (my speakers were definitely on :p).

    Thank you
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Your folder is named Ressources instead of Resources so nothing is being loaded into the array.

    You also have 2 AudioSources on your GameObject, which isn't necessary.
     
  7. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey,

    Yes i figured i tout, thank you ! :)
    Now i would like to pick a music from the array, and then remove it from the array (or at least it doesn't have to be available anymore).

    I was thinking of removing Choice1 from the Random.Range() but i couldn't find how to do it.
    I tried with a do while loop (while Choice2 = Choice1 pick a random number). It works.
    Not sure it is the best way to do it... ?

    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2.  
    3.     public AudioClip[] audioArray;
    4.     public AudioClip music1;
    5.     public AudioClip music2;
    6.     public AudioClip music3;
    7.     private int Choice1;
    8.     private int Choice2;
    9.     private int Choice3;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.         int Choice1 = Random.Range (0, audioArray.Length);
    15.         Debug.Log (Choice1);
    16.  
    17.         do{
    18.             Choice2 = Random.Range (0, audioArray.Length);
    19.  
    20.         }while (Choice2 == Choice1)
    21.                         ;
    22.  
    23.         do{
    24.             Choice3 = Random.Range (0, audioArray.Length);
    25.          
    26.         }while (Choice3 == Choice1 || Choice3 == Choice2)
    27.                         ;
    28.  
    29.         audioArray =  new AudioClip[]{
    30.             (AudioClip)Resources.Load("son1"),
    31.             (AudioClip)Resources.Load("son2"),
    32.             (AudioClip)Resources.Load("son3"),
    33.         };
    34.  
    35.         music1 = audioArray [Choice1];
    36.         music2 = audioArray [Choice2];
    37.         music3 = audioArray [Choice3];
    38.         audio.PlayOneShot (music3);
    39.  
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update () {
    44.  
    45.         Debug.Log (Choice3);
    46.      
    47.     }
    48. }
    Thanks,
     
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    To go for exactly what you are saying it would be like this
    Code (CSharp):
    1. public AudioClip[] audioArray;
    2.     public AudioClip music1;
    3.     public AudioClip music2;
    4.     public AudioClip music3;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.         audioArray =  new AudioClip[]{
    10.             (AudioClip)Resources.Load("son1"),
    11.             (AudioClip)Resources.Load("son2"),
    12.             (AudioClip)Resources.Load("son3"),
    13.         };
    14.  
    15.         List<AudioClip> tempClips = new List<AudioClip>(audioArray);
    16.         int index = Random.Range(0,clips.Count);
    17.         music1 = tempClips [ index ];
    18.         tempClips.RemoveAt( index );
    19.  
    20.         index = Random.Range(0,clips.Count);
    21.         music2 = tempClips [ index ];
    22.         tempClips.RemoveAt( index );
    23.  
    24.         index = Random.Range(0,clips.Count);
    25.         music3 = tempClips [ index ];
    26.         tempClips.RemoveAt( index );
    27.  
    28.         audio.PlayOneShot (music3);
    29.     }
    30. }
    But it would be tempted to just load then shuffle the array, then always take the first element (or last)
     
  9. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Thank you ! I don't know what tempclips are and how index works, so i'll keep using my script for now :)
    I'll definitely take a look at your code when i'm less tired !

    I'm trying to access one of these random audioclips from another script and it don't work :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Planet1Sound : MonoBehaviour {
    5.  
    6.     private AudioClip myPlanetSound1;
    7.     private GameObject myCamera;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.                   myCamera = GameObject.Find ("myCamera");
    12.               }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.             myPlanetSound1 = myCamera.GetComponent<GameManager> ().music1;
    18.  
    19.             Debug.Log (myPlanetSound1);
    20.  
    21.     }
    My Debug.log is always null. I dont understand why.
    I define music1 at runtime in my GameManagerScript, so it shouldn't be null...
     
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Log out myCamera, to see if the object is being found
    then log out the getted component to check that, etc