Help destroying a seperate game object on collision enter
Hi I have a script that plays an audio clip once when I enter the collider. For one instance I want to destroy a game object in my scene that isn't connected to the audio object. The script so far:
using UnityEngine;
using System.Collections;
public class PlayAudioDESTROY : MonoBehaviour {
private AudioSource audio;
private bool hasPlayed = false;
void Start () {
audio = GetComponent <AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (audio.isPlaying == false && hasPlayed == false)
{
audio.Play();
hasPlayed = true;
}
}
}
I want to add code to this that allows me to drag a different game object onto this script in the inspector so when I walk through it, it destroys said game object. Anybody know how to do this?
many thanks.
Answer by hexagonius · Jan 20, 2016 at 10:09 PM
create a public gameobject variable at the top. It will show up in the inspector for assignment. whatever you call it, call Destroy(saidGameObject) where you need it.
Hi mate thanks for the reply, i'd like it to destroy as soon as the player enters the collider. its weird, wont let me add the script because it says there are compiler errors when I put it under hasPlayed = true; but nothing shows up in the console. Thats a new one!
where would be a good place to put the Destroy(gameObject) call?