E. W
E. W

Reputation: 21

How do I turn off a specific Audio when my player dies?

So I'm making a Ball game where the goal is to dodge obstacles and reach the end of the level.

I've made a wind effect sound for when the player reaches a certain amount of speed.. however, when the player hits an obstacle and dies, the sound of the wind continues.

My question is: How do I turn off a specific Audio when my player dies? I'm not quite sure what functions & stuff I should put in my code.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleCollisionBig : MonoBehaviour
{
    public GameObject destroyedObstacle;

    void OnCollisionEnter(Collision collisionInfo)
    {
        if (collisionInfo.collider.tag == "Player")
        {
            Instantiate(destroyedObstacle, transform.position, transform.rotation);
            FindObjectOfType<AudioManager>().Play("ShatterBig");   
            Destroy(gameObject);    
        }  
    }
}

If I wasn't clear enough, feel free to ask about more information.

Thanks in advance, E.W

Upvotes: 2

Views: 408

Answers (1)

andeart
andeart

Reputation: 951

At the end of the day, you would need to grab a reference to your AudioSource object and call .Stop() on it.

As @SHAI might be suggesting, perhaps your AudioManager already has this functionality wrapped away in it (or if not, can be added to it).

Upvotes: 1

Related Questions