Using Tags

Given the challenge to stop the Hits counter from increasing when the player collides with a body that it has already collided with, I understand using the if(other.gametag != Hits) condition, but before this was given, I tried using my own method for this challenge and went with: 2 different methods. One that didn’t work out, and one that did, but might have caused troubles later down the track.

The one that failed:
if(other.gameObject.tag == “Hit”)
{
hits = + 0
}

and the other:
if(other.gameObject.tag == “Fresh”)
{
hits++;
}
(Objects would then change tags from Fresh → Hit tag)

Was there any way with my first method that could have gotten the code to work, and was it possible that method two would have caused an issue further down the line? (For example, needing to have used a different tag for the objects, and only being able to assign them one tag at a time?)

Cheers,

Hi @Zentisca! Welcome to the community

Is that the code you used? Is the tag 'Hit" or “Hits”? Also, the hits count would be set to 0 with that because hits = +0 is the same as hits = 0. You probably wanted hits += 0 which would just add 0 to the hit counter

I think for this game it’s fine to change the tag. You must just always be aware that a tag could have changed and is no longer the tag you think it may be.

1 Like

Thanks for the warm welcome!

Also thanks for the fast reply

Sorry I should have cleared that up, the “Hit” is the tag, and “hits” is the variable
I just changed the code then from the hits = + 0, to hits =+ 0 and in the log it would:
Event: Player hits wall
Log: You’ve bumped into something this many times: 1 (1 instance)
Event: Player hits different wall
Log: You’ve bumped into something this many times: 2 (1 instance)
and continues for each object, but also still increases the amount of times it’s mentioned in the Log for hitting something 1 time, when making collision* with an object that’s already been tagged as “Hit”

I think I understand the reason it’s doing that though, because it still has a value attached to it

I did read your reply wrong at first glance when the above was happening, and changed it to what you suggested hits += 0, but that was still adding 1 when an object was tagged as “Hit”

I know I’m paying A LOT of detail to something that’s easily fixed by just changing it to the !=, but it’s nice to see if my way of thinking would “work,” for when I take on bigger challenges without being able to check it right away with a tutorial

Could you post the code? hits += 0 is adding 0 to the hits. hits should not be incrementing. I suspect there may be other code that’s incrementing the hit, or the tag isn’t actually ‘Hit’.

I personally favor other.CompareTag("Hit") over other.tag == "Hit" because other.tag == "Hit" is just a string compare, but other.CompareTag("Hit") will throw an exception if the tag does not exist. This is just an extra little measure to ensure that you didn’t accidentally typed the tag wrong

1 Like

Sure, I can’t say I know how to upload the code in the formatting from Visual Studio, but will past it below
Don’t mind the //comments, that was from testing which method was going to work

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Scorer : MonoBehaviour

{

//other gameObject.tag = “Hit”;

int hits = 0;

private void OnCollisionEnter(Collision other)

{

    if(other.gameObject.tag == "Hit")

    {

    hits += 0;

    Debug.Log ("YOU'VE ALREADY HIT THIS OBJECT");

    }

    //if(other.gameObject.tag == "Fresh")

    //{

    hits++;

    Debug.Log ("You've bumped into something this many times: " + hits);

    // }

       

}

}

This code always runs. Even if the gameObject’s tag is ‘Hit’.

For your way to work, you will need to stop running this function once you have determined that it is a “Hit” object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scorer : MonoBehaviour
{
    //other gameObject.tag = “Hit”;
    int hits = 0;

    private void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.tag == "Hit")
        {
            hits += 0;
            Debug.Log ("YOU'VE ALREADY HIT THIS OBJECT");
            return; // <- Here you will exit the function because you are done
        }
        //if(other.gameObject.tag == "Fresh")
        //{
        hits++;
        Debug.Log ("You've bumped into something this many times: " + hits);
        // }   
    }
}
1 Like

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

Privacy & Terms