I can’t seem to find out what I’m doing wrong despite how many tutorials and forums I look through. It will play the sound if I use the Play on Awake checkbox. I put a debug log before the audio playing and see a message so there’s not a problem with my logic.
using UnityEngine;
using System.Collections;
public class Box : MonoBehaviour {
public string ID;
public AudioClip wrongSound;
private bool Good;
private AudioSource source;
// Use this for initialization
void Awake () {
source = GetComponent<AudioSource> ();
Good = false;
if (ID == "Distractor") {
Good = true;
}
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == "Red") {
if (ID == "RedBox") {
Good = true;
} else {
Good = false;
}
}
if (col.gameObject.tag == "Blue") {
if (ID == "BlueBox") {
Good = true;
} else {
Good = false;
}
}
if (col.gameObject.tag == "Floor") {
if(Good == false){
source.PlayOneShot(wrongSound,.5f);
}
Destroy (gameObject);
}
if (col.gameObject.tag == "BeltEnd") {
if(Good == false){
}
Destroy (gameObject);
}
}
}
