1
votes

I'm making a Unity game, in which player should push all "Enemy" object from the plane. So to be able to count the number of fallen objects I want to generally be able to tell when a collision occurred between the red cube and every other cube. The script seems to not detect a collision, how to fix it?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collide : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Enemy")
            Destroy(gameObject);
                Debug.Log("Hit Occured");
    }

}

View

View

View

3
did you set the collider as a trigger? have you checked the trigger/collider chart? did you mean trigger or did you want collision? - BugFinder
I want to see a Debug.Log message after a collision between the red cube and every other cube - Atanas
so not answering the question.. - BugFinder

3 Answers

4
votes

you need OnCollisionEnter

void OnCollisionEnter(Collision collision){

}

because your colliders aren't triggers.

1
votes

You need to implement OnCollisionEnter(Collision collision) not OnTriggerEnter(Collider other) or check BoxCollider IsTrigger checkbox

0
votes

There are 3 things to be checked 1. The OnCollisionEnter should be used in place of OnTriggerEnter 2. isTrigger checkbox should be enabled so that the event is triggered when the both bodies collide with other . 3. The most important thing which no one has mentioned is the tags given to the gameobject or the enemies because we need to define the gameobject that event should be triggered when hit to the specific body because the gameobject contains the collider and can collide to any wall or something so you need to define the tags properly