About 'Count Breakable Blocks'!

bump, still curious as to why putting count breakable blocks in the block script itself doesnt work.

If you declared a blockCount variable in the Block class, the variable belongs to the instance. If you have 20 Block instances, you also have 20 different blockCount variables. This is the only explanation that comes to my mind without knowing anything about your project.

Ok that makes sense, so rather then having one variable that = 20 when we have it in the level manage script I get 20 variables that = 1.

1 Like

Exactly. :slight_smile:

Everything works, but i dont understand why the “Serialized Field” is written exactly as “Breakable Blocks #”.
This really confuses me, because we have not written that exact two words with the space in between anywhere.in the code. Do i miss something ?

Hi,

Unfortunately, I’m not sure what you mean. Could you share the relevant code snippet? Generally, everything must always be spelt in the expected way in the C# script. Names may not have an empty space.

Hey, i hope following pictures explain my Point, its not a big thing:

but in the code its written without space, see here:
grafik
while in the editor its written with a space “Breakable Block”, see here:

Now I get what you mean. Thanks.

This is a feature of Unity. Instead of using the variable name as it is, Unity splits it up and starts each word with a capital letter. This behaviour cannot be changed.

If you tried that in C#, you would get an error.

To expand on what Nina said, as long as you are following the proper naming conventions, Unity can parse the variable names and present something more readable in the Inspector.

For example, by convention, variable names do not have spaces but instead use “camel-case,” that is, a capital letter to denote the start of each word. Of course, variables begin with a lower-case letter, but that’s okay.

So when you enter thisIsAVariable in your C# script, Unity knows that each time it finds a capital letter, it is the beginning of a new word. So you see This Is A Variable in Unity instead of your actual coded variable.

Rick I need help !!
in the inspecter for me the number of breakable objects keeps on increasing forever … all my code is correct idk y it still breaks!

Hi Tarun,

Welcome to our community! :slight_smile:

If you have a problem, please feel free to create a new thread. Describe your problem there and share as much relevant information on what you did and have as possible.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Make sure that there is no semicolon behind the if-condition.

I mean something like this:

if (condition);
{
    // code
}

Really having hard time digesting this lecture. If I got it right. Unity is not looking for Block when trying to count them, its looking for Level property each Block object has and is counting those. Its like you have x blocks in box and need to count them. Each block has white dot on it. For some reason you cant see how many blocks are in the box but you can see all the dots so you just count those. Am correct to asssume we could used any object we made so far. i.e. Paddle - create method for counting blocks there. Than in Block class we could cached Paddle variable and look for all objects of type Paddle and count those. Not saying this is better way or that it should be done this way just testing my understanding of this lecture. Thank you.

I tried before you to find a solution and did it like this wonder if its optimal

public class Level : MonoBehaviour
{
    [SerializeField] SceneLoader sceneLoader;

    // Update is called once per frame
    void Update()
    {
        if (GameObject.Find("Blocks").transform.childCount <= 0)
            sceneLoader.LoadNextScene();
    }
}

where the “Blocks” is the name of the empty object I’ve put all the breakable block instances to, and then used the scene loader function to go next level.

Hi Abysson,

Welcome to our community! :slight_smile:

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition. The only flaw I see is that the Find method is called in Update. Find methods such as Find and FindObjectOfType are one of the slowest methods in Unity, and it is not recommended to call them in Update. You had the right idea, though, which is great. :slight_smile:

me too, how can you resolved this?

resolves to me, i found the zombie block

Privacy & Terms