This question was
closed Jul 14, 2019 at 07:02 PM by
lCedric.
Delete object with key within Trigger
Hello! Here i have a script that is supossed to work, but it doesn't! What i am trying to do is: I have an object marked with a trigger collider, when my character enters the trigger, i have an option to press "E" on my keyboard, and if i press it, every object that has the "Delete" scipt attached to them will be deleted. Note that the "Delete" script has nothing in it, it's just an empty script. I am new to Unity so please try to explain it carefully! Thans for your help.
My script (c#) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : MonoBehaviour
{
public AudioClip SoundThatPlays;
AudioSource audio;
void Start()
{
audio = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Delete>() )
{
if (Input.GetKeyDown(KeyCode.E))
{
audio.PlayOneShot(SoundThatPlays, 1F);
Destroy(other.gameObject);
}
}
}
}
Comment
Answer by Bunny83 · Jul 13, 2019 at 11:29 PM
void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown(KeyCode.E))
{
if (other.GetComponent<Delete>() )
{
audio.PlayOneShot(SoundThatPlays, 1F);
Destroy(other.gameObject);
}
}
}