1

I am writing a script that shows a collision between one object and an obstacle. The code runs but does not output within the console. Is there something that I am doing wrong?

using UnityEngine;

public class collision : MonoBehaviour{

    void OnConllisionEnter (Collision collisionInfo)
    {
        if(collisionInfo.collider.tag == "Obstacle") 
        { 
            Debug.Log("We Hit an obstacle!");
        }
    }
    
}

I added a tag to the object as I will be adding more obstacles to simplify the process. I checked for semicolons and any other errors that would stand out to me. I am not sure what I am supposed to change or if I am missing something.

2 Answers 2

2

You actually have a typo in the methods name

//       Here
//        |
//        v
void OnConllisionEnter (Collision collisionInfo)

The code should be

using UnityEngine;

public class collision : MonoBehaviour{

    void OnCollisionEnter (Collision collisionInfo)
    {
        if(collisionInfo.collider.tag == "Obstacle") 
        { 
            Debug.Log("We Hit an obstacle!");
        }
    }
    
}
Sign up to request clarification or add additional context in comments.

Comments

1

When using OnConllisionEnter, check your collider component to make sure the [Is Trigger] flag isn't checked Collider Component, Is Trigger Flag

Also, remember that both objects have to have a collider component attached to them and only one of them will need a rigid body component attached to it (both of which have to match the correct world space, i.e. 2D/3D RigidBody and Collider.

More about Collisions: https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.