0
\$\begingroup\$

How can I play two different audio clips in Unity depended on the code behind? Into my object I've added two audio sources namely 'Player GunShot' and 'outOfAmmo'.

Into the same object I've also added a C# script whit this code:

public currentBullets = 1;

private AudioSource gunAudio;
private AudioSource outOfAmmo;

void Awake()
{
    gunAudio = GetComponent<AudioSource>();
    outOfAmmo = GetComponent<AudioSource>();
}

void Update()
{
    if (currentBullets == 0)
    {
        outOfAmmo.Play();
    }
    else 
    {
        gunAudio.Play();
    }
}

Can this code give any problems because in the Awake methode? I've not defined with audio source the variable must be. So which source go he take to play?

\$\endgroup\$
  • \$\begingroup\$ Have a look at this link, it will help you \$\endgroup\$ – Hamza Hasan Dec 30 '15 at 10:31
3
\$\begingroup\$

Try something like this (use only one AudioSource):

public currentBullets = 1;

private AudioSource gunAudio;
public AudioClip gunshot; //set this in ispector with audiofile
public AudioClip outofammo; //set this in ispector with audiofile

void Awake()
{
    gunAudio = GetComponent<AudioSource>();

}

void Update()
{
    if (currentBullets == 0)
    {
       gunAudio.clip = outofammo;
    }
    else 
    {
       gunAudio.clip = gunshot;     
    }
   gunAudio.Play();
}

Reference : here

\$\endgroup\$
1
\$\begingroup\$

Have public variables which you assign the sound files to in the script, then change the audioclip of audio source using the variables the sound clips were assigned to.

Look at the code given in the answer to this question for how to write your script: http://answers.unity3d.com/questions/347657/play-audioclip-once-and-switching-clips.html

\$\endgroup\$
-1
\$\begingroup\$

Public fields

 public AudioSource  aud_jump;
 public AudioClip  clip_jump;

 public string SourceNormalPlatJump = " set path in inspecter like "Sounds/jump_11" in resource folder ";
 public string SourceDestroyPltJump = "set path in inspecter like "Sounds/jump_11" in resource folder";



Start()
{
    aud_jump = GetComponent<AudioSource>();
}

Conditions

private void OnTriggerEnter2D(Collider2D hitPlatform)
    {

            if ((hitPlatform.gameObject.tag == "Platform" ) && rigidbody2DPlayer.velocity.y <= 0.2)
            {
               clip_jump = (AudioClip)Resources.Load(SourceNormalPlatJump);
                aud_jump.clip = clip_jump;
                aud_jump.Play();

               }

            // player hits Desroy paltform
            if (hitPlatform.gameObject.tag == "DestroyPlatform" && rigidbody2DPlayer.velocity.y <= 1)
            {
                clip_jump = (AudioClip)Resources.Load(SourceDestroyPltJump);
                aud_jump.clip = clip_jump;
                aud_jump.Play();
             }
  }
\$\endgroup\$
  • \$\begingroup\$ Please don't just drop some code. Please add some explanation how and why your code solves the problem. We are here to teach people, not just to fix their current problem. Give someone a fish, and you feed him for today. Teach someone how to fish, and you feed him for life. \$\endgroup\$ – Philipp Jul 4 '17 at 14:34
  • \$\begingroup\$ Paths to resource folders have a few disadvantages: 1) The user has to key them in and validate them manually, since Unity doesn't expose a "path" field. 2) If the resource file moves or changes name, the behaviour breaks. 3) Unity can't determine in advance which resources will be used, so it needs to pack them all into your executable even if you only reference a few of them. 4) Unity doesn't know which clips to have ready in memory until the moment you try to load & play them, which can cause a delay. In all of these regards, exposing an AudioClip property is more robust. \$\endgroup\$ – DMGregory Jul 4 '17 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.