Advance Colision Detection
Hi there, I have a box that whenever other boxes come into contact with that box they get destroyed. Let me clarify the situation first. I have a box. Whenever this box falls on the ground it gets destroyed. My box class has the following properties:
public void KillObject(){
Destroy(this.gameObject);
}
Now I want to do the following in the update method. If this game object meets with the given game object, killObject();.
void update(){
if (this.colider(other.colider) == true)
killObject();
}
Answer by UnityCoach · Apr 21, 2017 at 06:22 PM
You can't do it this way. You need to use
void OnCollisionEnter (Collision col)
{
if (col.### == ###) //replace ### with the properties you want to check for
killObject();
}
You can find all information of the Collision data here.
I didn't understand what you intended to do with this.colider(other.colider) == true
I meant if a box collider2D meets another box collider2D the gameObject gets destroyed.
Also I have tried your way, and have debugged it. It seems that it is not running.
Here is the full code I have sofar,
public class FloatingTiles : $$anonymous$$onoBehaviour {
public Collision col;
// Use this for initialization
void Start () {
}
void Update(){
}
void OnCollisionEnter (Collision col)
{
Debug.Log (col.gameObject.name);
if (col.gameObject.name == "FieldObject_FinishLine")
$$anonymous$$illObject();
}
void $$anonymous$$illObject(){
Destroy(this.gameObject);
}
}