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
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.