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

PlaySound method with Array.

Discussion in 'Scripting' started by PatrickLuchies, Jun 2, 2015.

  1. PatrickLuchies

    PatrickLuchies

    Joined:
    Jun 2, 2015
    Posts:
    5
    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. Twistplay

    Twistplay

    Joined:
    Dec 6, 2012
    Posts:
    36
    the audio clip isn't a valid parameter for the Play method.

    You should do either do this:

    audiosource.clip = geluid [clip];
    audiosource.Play ();

    Or use the PlayOneShot method instead, which allows you to pass in a clip to play in a "fire and forgot" style.
     
    PatrickLuchies likes this.
  3. PatrickLuchies

    PatrickLuchies

    Joined:
    Jun 2, 2015
    Posts:
    5
    This solved one of the problems.
    tyvm!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    audiosource => cd player
    audioclip => track on cd

    select track, push play on player
     
  5. PatrickLuchies

    PatrickLuchies

    Joined:
    Jun 2, 2015
    Posts:
    5
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AudioClass  {
    6.  
    7.     public void PlayAudio(int clip)
    8.     {
    9.         AudioSource audiosource;
    10.         AudioClip[] geluid;
    11.         audiosource = Camera.main.transform.Find ("EmptyCamera").GetComponent<AudioSource> ();
    12.         //AudioSource audio = GetComponent<AudioSource> ();
    13.         geluid = new AudioClip[]
    14.         {
    15.             (AudioClip)Resources.Load ("geluid/ontplof") as AudioClip,
    16.             (AudioClip)Resources.Load ("geluid/spacesound") as AudioClip,
    17.             (AudioClip)Resources.Load ("geluid/snoop") as AudioClip
    18.         };
    19.         //audio.PlayOneShot (geluid [clip], 0.7f);
    20.         audiosource.clip = geluid [clip];
    21.         audiosource.Play ();
    22.     }
    23.  
    24.     public void PlayAudio(int clip,string AudioSourceString)
    25.     {
    26.         AudioSource audio;
    27.         AudioClip[] geluid;
    28.  
    29.         audio = Camera.main.transform.Find (AudioSourceString).GetComponent<AudioSource> ();
    30.         geluid = new AudioClip[]
    31.         {
    32.             (AudioClip)Resources.Load ("geluid/ontplof") as AudioClip,
    33.             (AudioClip)Resources.Load ("geluid/spacesound") as AudioClip,
    34.             (AudioClip)Resources.Load ("geluid/snoop") as AudioClip
    35.         };
    36.         //audio.PlayOneShot (geluid [clip], 0.7f);
    37.         audio.clip = geluid [clip];
    38.         audio.Play ();
    39.     }
    40.  
    41. }
    42.  
    Made it work by creating a new class.
    Thank you for all the replies.