U
U
UnityMakar2019-01-13 15:18:03
Unity
UnityMakar, 2019-01-13 15:18:03

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);
    }
  }
}

Here are the ingredients of the stone:
5c3b2ec53a65d286596794.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
p4p, 2019-01-13
@UnityMakar

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 question

Ask a Question

731 491 924 answers to any question