Solved Dropper not Counting hit

Hello all. I just wanted to say I solved the issue of dropper not registering as a hit. There’s a race condition with the decoupled design. When I debugged and stepped through, Unity was calling the ObjectHit.OnCollisionEnter() first which was tagging it as “Hit”. So when Scorer came along right after and checked if it had the tag of “Hit”, it was true so it was not incrementing the count.

The solution I came up with actually solved ground collision also for me without special code. I basically just moved the collision code on Scorer to my own method called AddCollision() and then in the ObjectHit code, after the color change and tag code, called the Scorer AddCollision() method from there. A side benefit of this solution is that it solved the ground plane collision since that does not have an ObjectHit component. Here’s my code below. Also, bonus, I used String Interpolation to make the message easier to read which is a popular way now to make strings that have a lot of fields/variables in them.

3 Likes

A bit late to the party but thank you for this, I was having the exact same problem

1 Like

Thanks. This is helpful. I was having the same issue and it seemed like it was being tagged before it was being evaluated. I like your solution. I ended up following another thread on here in which someone said they recreated the dropping object. I did that and it fixed it so I guess I just applied the scripts in the correct order.

Now I’m just trying to figure out how to make the player cube not slide about when it hits things!

1 Like

Very nice :+1:t4:

I think a simpler approach is to just delegate the tag change to the scorer itself.

if (collision.gameObject.tag != “Hit”)
{
numOfCollisions++;
Debug.Log($“Bummped into {numOfCollisions} obstacles.”);
collision.gameObject.tag = “Hit”;
}

This fixed the race condition for me as objectHit is responsible for changing the object’s material color and then scorer is responsible for keeping score and determining if it has seen an object before by checking and marking.

1 Like

Privacy & Terms