Play sound but only not on the first OnTriggerEnter

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;
        }
    }


}

Is it set to play on awake?

No, the Audio Source is not set to play on awake.

I would use a bool to specify which is the first pad myself.

using UnityEngine;
using System.Collections;

public class LandingPlayerAU : MonoBehaviour
{
    public AudioClip[] landingListAU;

    private AudioSource _audioSource;

    public bool firstPad; //Tick this in the inspector on your firstPad.

    void Start()
    {
        _audioSource = GetComponent<AudioSource>();
    }

    void OnTriggerEnter(Collider colEnter)
    {
        if (colEnter.tag == "Player" && !firstPad)
        {
            _audioSource.clip = landingListAU[UnityEngine.Random.Range(0, landingListAU.Length)];
            _audioSource.Play();
        }
    }
}

or if you don’t plan on doing anything else in the script, you could just not attach the script to the firstpad/have a different script for it.

Thanks, got it. My script would have never worked because it was placed on an instantiated object. So it would have always been false. I came up with a new solution that solved the problem.