1

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? Commented Jan 30, 2019 at 8:32
  • I want to see a Debug.Log message after a collision between the red cube and every other cube Commented Jan 30, 2019 at 8:54
  • 2
    so not answering the question.. Commented Jan 30, 2019 at 9:00

3 Answers 3

4

you need OnCollisionEnter

void OnCollisionEnter(Collision collision){

}

because your colliders aren't triggers.

Sign up to request clarification or add additional context in comments.

4 Comments

I dont think so, because he want the red cube to push the others
True, I half missed that, but then you could want to add velocity by hand or something like it runs away or something..
Thanks, that solves a problem and also leads to another one. Now I receive from the console: The name 'other' does not exist in the current context. How to rewrite other.gameObject.tag == "Enemy" so that other means the object of collision?
now you must use "collision.", maybe its collision.collider.gameObject.tag?
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.