Destroy object on hover?
I have a mini game which has 4 objects and a gameobject that when is hovered over them, they should destroy themselves, but the script is not working properly.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DestroyTestCS : MonoBehaviour {
private int Seconds;
public bool isDestroyed = false;
private int count = 0;
public Canvas miniGameCanvas;
public Renderer rend;
void Start(){
rend = GetComponent<Renderer>();
rend.enabled = false;
}
void Update(){
}
void OnCollisionEnter2D (Collision2D col){
if (col.gameObject.tag == "laser") {
Destroy (gameObject);
}
}
public bool GetIsDestroyed(){return isDestroyed;}
}
I've tried OnTriggerEnter2D and OnCollisionEnter2D but none of them seem to work. Help is highly appreciated.
Answer by Deadshot1994 · Oct 21, 2015 at 11:27 AM
So for anyone interested, the way I achieved this was that I added Rigidbody2D to every object I needed.
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag == "laser"){
Debug.Log("works!");
Destroy(gameObject);
}
}
This is the script I used after figuring out what was not working.
Your answer
Follow this Question
Related Questions
How do I destroy an instantiated game object when player leaves trigger area? 1 Answer
Can the new ParticleSystem Trigger Module access the particle's collision collider? 0 Answers
I'm using a Trigger for a projectile and I'm having trouble getting everything to work properly 0 Answers
Unity 2D jump "fly" 0 Answers
How do I make this script play a particle sytem from prefab 1 Answer