OnCollisionEnter() function doesn't work


In the 19th lecture of “Developing 3D games with C# and Unity,” I did the same code written by the instructor, but I don’t think it’s possible in my Unity game. I think it’s because what’s shown in the image is not installed. Really? How do I solve this problem?

What error are you receiving?

Hi Su-Hyun,

Lines 1 and 2 refer to available namespaces that you have. However, they are darker than UnityEngine because you do not use anything from those namespaces in your code. Feel free to remove those two lines.

How do you know that the collision method does not work? Are the messages enabled in your console?

1 Like

Do both objects have colliders?

1 Like

Hey there is no “installation problem”: the first two using are greyed liked that because you’re not using them. You can even delete these first two lines and it won’t impact your code.

Issue is probably with your GameObject not having colliders in the scene editor…

1 Like

Yes, I added them all to the wall.

Yes, even if the player hits the wall, the message(“Bumped into a wall”) entered in script is not printed.
ezgif.com-gif-maker

@agnes1.23 on your wall piece, you need to include the ObjectHit script to that component. Should look something like this:

2 Likes

That was a silly mistake…Thank you!!

Glad to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.