I can't get my collisions to work. Is there something I missed?

this is my code:

using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
switch (other.gameObject.tag)
{
case “Friendly”:
Debug.Log(“This thing is friendly”);
break;
case “Finish”:
Debug.Log(“Congrats, yo, you finished!”);
break;
case “Fuel”:
Debug.Log(“You picked up fuel”);
break;
default:
ReloadLevel();
break;
}
}

void ReloadLevel()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex);
}

}
and, as the cherry glitch on top, my debug.logs aren’t working.

What do you mean when you say it’s ‘not working’? Are you not getting collisions? Are you getting collisions, but the code doesn’t do what you expect?

Things to always check when you don’t get collisions is:

  • Is there a RigidBody on at least one of the objects?
  • Is there a collider on both objects?
  • Is the collider marked as ‘Trigger’ (it shouldn’t be in this case)
  • Is the CollisionHandler script on the same game object as the collider?
1 Like

I’m not an expert, so I don’t really know what the last one meant.
But, all of those requirements are filled.
(Maybe not the last one (. _ .) )

You would normally have a hierarchy of GameObjects. Something like

Player
  +-Spaceship Model
    +-Guns Model
      +-Bullet Spawn Point

A collider would be on one of those game objects. A script that listens to OnCollisionEnter must be on the same game object as that collider, eg. if there’s a collider on the Spaceship Model above, the CollisionHandler script must also be on the Spaceship Model game object

yes in fact I do!
each of them (except light and camera) have a collider, yet it’s still not working.

Is the collision not happening, or is the code not doing what you expect?

Start with this

private void OnCollisionEnter(Collision other)
{
    Debug.Log("Collision success!");
}

if you get that message we know the collision is happening and all is well, and we can continue onto step 2

my debug.logs won’t work

Why? How do they not work?

I have absolutely no idea, but the collision worked!

what’s step 2?

So, does the Debug.Log work? How do you know the collision worked? Sorry, I just want to get in line with what we’re working with here

I used transform.Translate(0, 100, 0);
and it worked!

Hmmm, I would like to see the tags of the objects that collide, so Debug.Log would be ideal

Screenshot 2022-07-10 223032
Have you taken a look at these on your console and see if they are toggled off?

1 Like

well, I can’t find them

So, is your console closed? Go to Window -> Panels -> Console to open the console

I still can’t find them

all I see at the bottom is:
Screen Shot 2022-07-10 at 3.38.22 PM

hold up. I found them.

ok, cool. so have we got Debug.Log back yet?

1 Like

I agree with @bixarrio , is everything tagged properly?

Privacy & Terms