Can't get collision OR trigger events to print to console

I’m having an issue where I can’t get anything to print to the console regarding the “CollisionHandler” script from this lecture. I believe I have everything the same in the script as Rick. However, I noticed the curly braces around “this.name” and “other.gameObject.name” are showing as red, not yellow like on Rick’s. I’m not sure if that matters or not. I’m not getting any errors (see screenshot).

No matter what combination I try with having rigidbody on or off, or trigger on or off with either the player ship or the enemy ship, nothing happens. I’ve tried rewatching several times and can’t find where I’m going wrong. I tried reloading the project as well, but that didn’t help either.

Hi! Welcome back to the community!

I don’t think this is a code issue, check your prefabs and be sure that they are the same as Rick’s, same for your colliders and enemies, collisions are a little tricky to handle, so double-check that and keep in mind the collision matrix and how it works.

So I’ve re-watched the Collisions & Triggers lecture a few times and consulted the table Rick shows and I should be getting something in the console, but there’s nothing at all. What other things can I check that can go wrong if the prefabs are setup right and the script looks okay?

Edit: I tried creating a new project and made a few prefabs and the console IS showing a collision took place in that project.

I had this same issue - nothing was outputting to console. Restarting Unity didn’t fix it, but restarting Unity AND Visual Studio did fix it. Not sure what restarting VS has to do with compiling the code… but for whatever reason, that worked!

Hi Brandon,

Welcome to our community! :slight_smile:

Maybe the script was not saved properly. That sometimes happens.

1 Like

How are you getting on with this problem, @HaggardMelon?

Hi Nina,

I tried adjusting my animated enemy craft so I could crash into it to see if it would respond better than the placeholder enemy sphere. Unfortunately, I still can’t get any collisions with my player ship. If I have time today, I may have to try importing a new instance of the Starsparrow ship and start it from scratch. I feel like there must be a problem with that prefab. Unless you have any other things I could try with the ship first?

Thanks!

First of all, check if the messages are enabled in your console.

Secondly, check if your relevant game objects have got colliders attached. At least one of the colliders must be a non-trigger collider.

Thirdly, attach a Rigidbody to all moving game objects.

Theoretically, this should make your collision work, given the script is attached to one of the game objects involved in the collision.

I don’t see anything red in the sense of an error. If there is a compiler error, the part is usually underlined. The code looks fine to me, so I assume the problem is in Unity.

If you cannot make the script work, create two cubes with colliders and a Rigidbody. One of them should have this script attached. Then make them collide. If the messages appear, your script is working.

So I believe the console should be showing messages because if I fire my lasers at the enemies, they report being hit and disappear from the other script working normally.

I also made sure all non-player objects moving or not, have colliders and are not triggers.

I verified everybody has a Rigidbody attached.

I still am not seeing anything showing up in the console after that.

Lastly, I tried putting in two cubes, attached the script to one, gave them both colliders and a Rigidbody and made one of them a trigger. They both have gravity activated. I placed one cube above the other. The trigger cube passes through the other one and falls through the terrain. Still no messages showing up in the console.

So does this sound like something is up with the script? Should I try creating a new C# script and do it again from scratch to see if there’s just something going wonky with VS Code for this particular script for some reason?

EDIT: I also just noticed I’m no longer getting any Unity autocomplete suggestions from VS Code or red squiggles showing errors or any squiggles for that matter, even if there are errors. Just wondering if this would be affecting the scripts in any way.

Could you please share a screenshot of the entire console window? And which version of Unity do you use?

So does this sound like something is up with the script? Should I try creating a new C# script and do it again from scratch to see if there’s just something going wonky with VS Code for this particular script for some reason?

Creating a new script might be a good idea. At least, that’s something you should try. Don’t waste too much time on it. Just copy the OnCollisionEnter method from the API into your script and replace the code block with a simple Debug.Log.

That might well be the case. Some students experience “interesting” side effects with VS Code for no apparent reason. Go to Edit > Preferences > External Tools and try to click on “Regenerate project files” if that option is available.

Here are more suggestions:

Please follow the instruction on this website and make sure all required extensions are installed: https://code.visualstudio.com/docs/other/unity

If the issue persists, please check the console of VS Code (not Unity!). If the .NET Framework 4.7.1 (Developer Pack) is mentioned there, download and install it from the official Microsoft. Here is the link: https://dotnet.microsoft.com/download/dotnet-framework/net471

Depending on your version of Unity, it might be that you will have to install the “Visual Studio Code Editor” package in Window > Package Manager in Unity.

Maybe you’ll have to reboot your computer. Then launch Unity again and open one of your scripts.

If nothing helps, I would suggest to install Visual Studio (not Visual Studio Code). Select it as your External Script Editor in Unity (Edit > Preferences > External Tools). In the Package Manager, install the “Visual Studio Editor” if it’s not installed yet.

Okay thanks, Nina. I’ll try all this over the weekend if I have time and report back!

I tried everything you mentioned above other than switching to Visual Studio (non-code). I’ll probably try that next.

Interestingly enough, I made another script and typed it all out exactly the same as before and I started getting trigger printouts in the console, but still no collisions.

The Unity version I’m on right now is 2020.2.7f1 Personal.

I also added a screenshot of the console with the triggers showing up.

That’s a fairly old version. Maybe you are experiencing a bug. Try to update to the latest stable version of Unity 2020.

Seems that the system bumped this, but also looks like it wasn’t resolved.

I saw you mention animation. Are you animating the position of the model and it’s a child of the main gameObject… which might mean that the collider isn’t in the same position of where the object is, so the collision wouldn’t happen when you’d expect.

Just a guess.

I assume you haven’t gone into the settings and unticked some layers in the physics matrix?

This topic was automatically closed after 18 hours. New replies are no longer allowed.

Privacy & Terms