Issue with Detecting Interaction with an Object... Can Anyone Help?

Hi,

I’m trying to complete a tutorial on YouTube and I have run into an issue. I went back on the tutorial and can’t for the life of me find where it went wrong, as my code etc seems identical.

I’m at the stage where I’m creating a highlighted visual for an interactive object when walking up to it, but when I’m standing stationary next to it, it both registers the interaction and does not. (See console image of null and contact inputs occurring at the same rate) This leads to the highlighting visual flickering in and out, and repeatedly activating and deactivating in Unity.

So. Youre doing code monkeys :slight_smile:

So. I dont remember exactly how he did it. So can you briefly explain what you think hes using to do the identification

Yes, the Code Monkey Tutorial :smiley:

He’s using the Raycast to detect objects.

Hopefully the code will help.

yes, yes it does :slight_smile:
So, if you add debug statements about to show where it got to and what values it had, what is this showing you?

Do all your objects have colliders
What is the layer you set on the object youre expecting to be interacting with, and what settings do you have on it
what did you set the mask to?
Once you added debug statements, where did it deviate from your expected path?
Did you do setup the events?

All of my counters have box colliders.

Most of the debug statements I have tried at this stage of the tutorial show the mask to be activating deactivating, or my interaction to the counter to fluctuate on and off even when I’m standing right next to it.

One thing I have noticed is if I change the size of the mask compared to the object its masking, it seems to change the rate at which it fluctuates the detection, but it still does not solve it. Could it be to do with the scale etc of the objects, or positioning? Or even my characters movement in relation to this?

The events were setup in the previous part of the tutorial, but I do not get the outcome that he does.
He is able to walk up to his objects, have them highlight on and off as he moves towards and away from them, and also when walking on the Z and X axis too, which doesn’t happen for me.

I’ll take some more screens of the objects and their inspection window.

Counter

OK, you said the mask is fluctuating? I dont remember how he changes to show the highlighted and not, but it sounds like the highlighted is not using a collider

Also

Isnt on a counter layer… Id expect it to be so that when empty or full it responds

Hey, thanks for the help, still hasn’t been resolved though. I’m wondering if its to do with my character movement or something earlier in the tutorial. Unity blocked my replies on both forums and messages for 24 hours as new user with too many posts.

Here is a gif of issue, if its of good enough quality so you can visualise it. If not I’ll try and upload a better one or create a video link or something.
20250122_205818 (4)

well, a ton of people have done that codemonkey tutorial, and succeeded so the devil somewhere is in the details.

As for whether you missed something earlier… the best answer to that would be, make a note of where you are, and watch back checking everything, your toon seems to walk into the unit and stop, so in part some is working, so it suggests something has been missed. However, the specific of what, I doubt theres too many people who would work through the tutorial that far to try and play guess what you didnt do.

I also hear @Kurt-Dekker and his explaination of using tutorials, (tagging him to hope he pastes his lovely speach) so, dont blindly copy, if you dont understand what codemonkey did, you can probably have a bit more success with a very specific question such as at timestamp x, he does, this but i dont undersand why that works and he doesnt do y.

I only watched the course and its been a while, so I recognise it, but not too surprisingly didnt commit it to memory.

Im really surprised if you added a debug message at each stage you didnt find out why its not doing what you thought, because the messages should show you where it goes in the code, what values you get… and well by the end the answer should be apparent, now, it may raise a question of “why is this variable x when I expected y” and then you follow that route…

Thanks @bugfinders!! As bugfinders hints at, you gotta find the bug.

Bugs are found by debugging… and it is time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Here’s my two-step tutorial process:

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

It really does have to be tested and checked at each step ideally because if you miss something early on, it can compound and ramify future work and really lead you astray. That’s just how software engineering works.

Imphenzia: How Did I Learn To Make Games:

I debugged the whole lot a found it was within the if statements after the raycast detected an object. Turns out I just had an else statement in the wrong set of brackets, almost embarrassed with the time it took me, I’d even tried rearranging some else statements before.

I appreciate all the help.

1 Like