Answer the question
In order to leave comments, you need to log in
Why is OnTriggerEnter2D not responding in Unity?
Help me please!
I'm making a 2D game about a spaceship, there are rocks that the ship has to destroy. For some reason, the stone does not react to bullets and a barrier (they have the "Destory" tag). Here is the stone code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Control_Stoun : MonoBehaviour {
void Update () {
transform.position = new Vector2(transform.position.x, transform.position.y + -1 * 2 * Time.deltaTime);
transform.Rotate(transform.rotation.x, transform.rotation.y, transform.rotation.z + -1 * 40 * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("1");
if (collision.tag == "Destory"){
Destroy(gameObject);
}
}
}
Answer the question
In order to leave comments, you need to log in
1. The trigger must have the is trigger flag
2. To compare the tag, there is a collision.compairTag function. It's faster and doesn't clog memory.
3. One of the objects seems to be rigidbody
Yes, and better than checking tags, adjust the interaction of physics by layers.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question