After the audio clip is played, the game object is supposed to be destroyed so that it cannot play again, but it doesnt get destroyed and i have no idea why. Here is my code:
public AudioClip _MySound;
AudioSource _MySource;
public GameObject player;
void Update()
{
}
void OnTriggerEnter(Collider other)
{
GetComponent<AudioSource>().PlayOneShot(_MySound);
GetComponent<GameObject>();
if (other.gameObject.tag == "Player")
{
_MySource.PlayOneShot(_MySound);
Destroy(gameObject, _MySound.length);
}
}
Make sure the ontrigger event is actually getting called by either listening for the sound or adding a Debug.Log() in the trigger. If it seems like it’s not getting called make sure your player has a rigidbody component. Triggers require that one of the objects have a rigidbody and without it the trigger event won’t register. If you don’t want gravity after adding the rigidbody make sure to uncheck the use gravity checkbox on the rigidbody.
Also, if this happens to be a 2D game you should be using a Rigidbody2D and an OnTriggerEnter2D.
If the trigger is actually getting reached and called then the Destory() call should be working. You’re passing the game object that this script is attached to and the audio clip length which seems to be correct.