Obstacle keeps counting?

Hi, my obstacle keeps counting the score, while my other objects like the dropper and border walls don’t. Here’s the setup I’m using:

  1. The obstacle has Scorer.cs and ObjectHit.cs attached.
  2. The obstacle is tagged as Hit .
  3. The player is tagged as Player .

Here is my code:
(Scorer.cs)

> public class Scorer : MonoBehaviour
> 
> {
> 
>     // Start is called before the first frame update
> 
>    int hits = 0;
> 
>    private void OnCollisionEnter (Collision other)
> 
>    {
> 
>     if (other.gameObject.tag != "Hit")
> 
>     {
> 
>         hits ++;
> 
>         Debug.Log ("You've bumped into a walls this many times: " + hits);
> 
>     }
>    }

Object.Hit.cs

public class ObjectHit : MonoBehaviour
{
   private void OnCollisionEnter (Collision other) 
   {
    if (other.gameObject.tag == "Player")
    {
      GetComponent <MeshRenderer> () . material . color = Color.red;
      gameObject.tag = "Hit";
    }  
    
   }
}

Can you help me out? Thank you before

Remove other from here. Other is the player and the player never gets the “Hit” tag. So, it should just be

if (gameObject.tag != "Hit")

Hi thankyou for the reply, i changed the code, removed “other” into if (gameObject.tag != "Hit") it still doesnt work out

OK, I had this wrong;

  • Scorer.cs goes on the player, not on the obstacle.
  • It should be other.gameObject.tag != "Hit" (like it was before) because the player is checking if it hit the obstacle.
  • The obstacle should not be tagged with “Hit”. This is set by the ObjectHit.cs script when the player hits it.
  • ObjectHit.cs should be on the obstacle.

Check that these are correct

Nice explanation, it workked, thank youu

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

Privacy & Terms