Why aren't my blocks being counted in the Level game object?

Hello Rick,

I’m really enjoying the course so far, but I’ve gotten stuck on the ‘count breakable blocks’ part. I’m completely new to coding so I probably made an error at some point, but I’ve checked my code several times and it’s the exact same as in the video. When I press play in Unity, my blocks aren’t being counted in the Level game object. The Breakable Blocks section is there under the Level script component but just remains at zero. Any suggestions for where I might have gone wrong?

Thanks,
Rachel

Hi Rachel,

From memory, the Level script has a method named CountBlocks;

public void CountBlocks()
{
    breakableBlocks++;
}

This was being called from the Block script, in a method named CountBreakableBlocks, called from the Start method (may vary depending where you are in the course);

private void CountBreakableBlocks()
{
	level = FindObjectOfType<Level>();

	if (gameObject.tag == "Breakable")
	{
		level.CountBlocks();
	}
}

This process relies on a few things, firstly, the Block GameObjects require the Breakable tag to have been assigned in the Inspector, either individually, or, with multi-select, or by amending the prefab.

There is also the reliance of having a Level GameObject in the scene, as you can see from the CountBreakableBlocks method, it attempts to find it, although it if doesn’t you would expect to see a NullReferenceException error when level.CountBlocks() is called.

To diagnose the issue you are experiencing I would try the following;

  • check that the scene has a Level GameObject with the Level.cs script component attached
  • check that all of the breakable blocks in the scene have the Breakable tag set
  • check that you are calling the CountBreakableBlocks method from the Start method within Block.cs

If you are certain all of these steps have been taken, try amending the CountBreakableBlocks method within Block.cs as follows;

private void CountBreakableBlocks()
{
    Debug.Log("CountBreakableBlocks method called");

    level = FindObjectOfType<Level>();

    if (gameObject.tag == "Breakable")
    {
        Debug.Log("Breakable tag detected");

        level.CountBlocks();
    }
}

When run, for each block in your scene, you should see the message “CountBrekableBlocks method called” appear. Additionally, for each block which has its tag set to “Breakable” you should see the additional “Breakable tag detected” message.

In order to keep things readable in the console you might consider creating a new level with perhaps just the one breakable block and see what happens.

Let us know how you get on, if you don’t make any progress, share your project files and I will happily take a quick look for you :slight_smile:

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Hi Rob,

Thanks so much for your response. I tried what you recommended and am still having the same problem, so I’m not sure what to do now. My project files are a bit over 10MB, so here’s the Google drive link: https://drive.google.com/file/d/1eCpB_GH9vx9ERHI5QqPUSN0jcaMbG-Jz/view?usp=sharing

I really appreciate you looking them over. No rush of course, just whenever you have a chance.

All the best,
Rachel

Hi Rachel,

I’ve had a look at your project.

The problem is that on all of your blocks in the scene, the Block.cs script component is disabled, e.g. there is not a tick here;

image

So, your Block script isn’t enabled, this the code doesn’t run. Part of this code was to get a reference to the Level GameObject and then call it’s CountBreakableBlocks method. This doesn’t occur.

To resolve the issue, browse through to your /assets/prefabs folder within the Project View and then enable the Block.cs script component on the Block prefab. All of the prefab instances in the scene will be updated and are then enabled.

Run the game - 24 blocks. Enjoy :slight_smile:

Thanks Rob. I thought it might be something simple like that I just couldn’t see. I’ll know to check all the boxes are ticked for next time!

Best,
Rachel

1 Like

You’re very welcome Rachel, I’m glad you can move forward again with the course :slight_smile:

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

Privacy & Terms