Breakable tag is not work when I try to check it in "Level" script

In the video Breakable tag is checked in the “Block” script, my game can work when I make the same code in the video.
When I make the challenge, I check the tag in “Level” script, my code is

public void CountBlocks()
{
    //breakableBlocks++;
    if (tag == "Breakable")
    {
        breakableBlocks++;
    }
}

In the Block my code is

void Start()
{
    level = FindObjectOfType<Level>();
    //if (tag == "Breakable")
    //{
    //    level.CountBlocks();
    //}
    level.CountBlocks();
    gameStatus = FindObjectOfType<GameSession>();
}

When I try this, the count is not work.

image

I don’t know what is wrong in the code, hope someone can help me.
Thanks.

Hi Booow,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Does the if-block in CountBlocks get executed? If it does, log the value of breakableBlocks into your console.

If you see the expected value, make sure that you did not select the prefab in your Assets folder. During runtime, Unity works with the game objects in your Hierarchy. That’s why you have to select the game object with the Level component in the Hierarchy.

Did this help?


See also:

I try to put a log message in the CountBlocks function

public void CountBlocks()
{
    //breakableBlocks++;
    if (tag == "Breakable")
    {
        breakableBlocks++;
        Debug.Log("block+1");
    }
}

There is no message show up.
Then I try to put the log outside of the if-block

public void CountBlocks()
{
    //breakableBlocks++;
    if (tag == "Breakable")
    {
        breakableBlocks++;
    }
    Debug.Log("block+1");
}

The message is show up

It looks like the if(tag == “Breakable”) is not work.
I try to find what different between my work and the video work, and I think it might because I check the tag in the “Level” script which it is not a component in the Block object.
Is that right or fault, and if it is right, how can I check the tag in other script.

Well done so far. Since the message appears when it is not inside the if-block, we know that the code works as expected except for the if-condition. That’s good news because we don’t have to look “everywhere” in Unity anymore.

Check your Block game objects. Do all of them have the “Breakable” tag assigned?

You could log their tag into your console during runtime:
Debug.Log("tag: _" + tag + "_");

I added “_” because it might be that there is a space after the tag which prevents the condition from getting evaluated to true. The underscores mark the borders of the tag string.

By the way, in which script is the CountBlocks method, and to which game object is that script attached? I’m wondering if there is some misunderstanding. Objects do not know which other objects called their methods, thus they cannot know anything about the other objects.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

The Block is set the tag of breakable, it can work if I use the lecture’s code.
Looks like the problem is because the “Level” script is under the Level game object, and the tag is under the Block game object.
I have try to add a parameter to the CountBlocks function like

public void CountBlocks(GameObject gameObject)

I try to check the tag by use gameObject.tag, it works.
Thanks for your help, I will keep working hard. :grinning:

That was the problem I suspected. tag without anything and without any tag variable within the same script refers to the component. And since your class inherits from MonoBehaviour, tag also refers to the game object to which your component is attached. That’s the tricky part in C#: Understanding where the data assigned to variables comes from because that’s not always obvious.

Method overloading is definitely a way to solve your problem. Good job on figuring this important technique out by yourself! :slight_smile:

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

Privacy & Terms