how to stop a public audiosource from being called in-game in csharp

You can stop a public AudioSource from playing by calling the Stop() method on the AudioSource component.

Here's an example of how you can stop a public AudioSource called myAudioSource from being played in a game:

main.cs
using UnityEngine;

public class AudioController : MonoBehaviour
{
    public AudioSource myAudioSource;

    // Method to stop the audio source
    public void StopAudio()
    {
        if (myAudioSource != null && myAudioSource.isPlaying)
        {
            myAudioSource.Stop();
        }
    }
}
303 chars
16 lines

In your game code, you can call the StopAudio() method to stop the AudioSource from playing:

main.cs
audioControllerScript.StopAudio();
35 chars
2 lines

This will check if the AudioSource is not null and is currently playing, then it will stop the audio.

related categories

gistlibby LogSnag