So basically when you collide with a trigger it will play a audio but when you are no longer in that trigger it will stop playing the audio, however it is not working…
using UnityEngine;
using System.Collections;
public class PlayAudio : MonoBehaviour
{
void OnTriggerEnter(Collider otherObj)
{
if (otherObj.tag == "Player")
{
GetComponent<AudioSource>();
}
else
{
AudioSource.Stop();
}
}
GetComponent<AudioSource>(); just gets the component of type AudioSource and it does nothing with it. If instead you would write GetComponent<AudioSource>().Play(); it will play the sound that is assigned as a clip, in the inspector window.
Same goes for the else statement, instead of AudioSource.Stop();, you should write 'GetComponent().Stop();`
Basically, with GetComponent, you are getting the component that is attached to the same GameObject that the script is attached to. Once you do that, you need to tell it to execute certain methods (in this case Play() and Stop() methods)