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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

PlaySound method with Array.

Discussion in 'Audio & Video' started by PatrickLuchies, Jun 2, 2015.

  1. PatrickLuchies

    PatrickLuchies

    Joined:
    Jun 2, 2015
    Posts:
    5
    For starters, I am new to this forum and i don't know how things are handled here so if I do something wrong just tell me instead of instant downvoting.

    I made method within C# Unity to play different sounds true array index values.

    This is my method:

    public static void PlaySound(int clip)
    {
    AudioSource audiosource;
    AudioClip[] geluid;
    audiosource = Camera.main.transform.Find ("EmptyCamera").GetComponent<AudioSource> ();
    geluid = new AudioClip[]
    {
    (AudioClip)Resources.Load ("geluid/ontplof") as AudioClip,
    (AudioClip)Resources.Load ("geluid/spacesound") as AudioClip
    };
    audiosource.Play (geluid [clip]);
    }
    I am trying to invoke this method from an exteral file by using: spawn.PlaySound(0);, the idea is
    that the the method is playing the sound with the given value.

    I keep trying these error messages and it isn't clear to me what i can do to make it work:

    Assets/spawn.cs(175,29): error CS1502: The best overloaded method match for `UnityEngine.AudioSource.Play(ulong)' has some invalid arguments

    Assets/spawn.cs(175,29): error CS1502: The best overloaded method match for `UnityEngine.AudioSource.Play(ulong)' has some invalid arguments.

    It keeps giving the error message at this line: audiosource.Play (geluid [clip]);

    Any help would be appriciated.
     
  2. PatrickLuchies

    PatrickLuchies

    Joined:
    Jun 2, 2015
    Posts:
    5
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    When posting code, please use code tags

    Play() doesn't take an audioclip as an argument.

    Code (csharp):
    1.  
    2. audiosource.clip = geluid [clip];
    3. audiosource.Play();
    4.  

    You should cache this in Start()
    Code (csharp):
    1. audiosource = Camera.main.transform.Find ("EmptyCamera").GetComponent<AudioSource> ();
     
    PatrickLuchies likes this.