Hi,
I’m trying to trigger a sound every time my player enters a Box Collider. So far, so good. On game start the player starts inside a Box Collider. That’s the only moment i don’t want the sound to be triggered. After the first trigger, so on the second trigger and beyond the sound should always trigger. I tried the following code, but it doesn’t seem to work.
Any suggestions what to do?
using UnityEngine;
using System.Collections;
public class LandingPlayerAU : MonoBehaviour {
public AudioClip[] landingListAU;
private AudioSource audioSource;
bool firstEnter;
// Use this for initialization
void Start () {
firstEnter = false;
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider colEnter)
{
if (colEnter.tag == "Player" && firstEnter == true) {
audioSource.clip = landingListAU [UnityEngine.Random.Range (0, landingListAU.Length)];
audioSource.Play ();
}
}
void OnTriggerExit (Collider colExit)
{
if (colExit.tag == "Player") {
firstEnter = true;
}
}
}