Block Count One Higher than Actual

I’ve come across a quirk that I can’t get past. After implementing the block count (start and post-destroy), I seem to have a phantom ‘block’ that I can’t find. I have added 22 blocks to my scene, and 22 appear in the hierarchy. Assuming that perhaps I’d made an error in placing them, I deleted all the blocks then created and placed all new instances.

Each colour block is its own prefab, and I’ve confirmed that the prefabs have the Block scripted added to them only once, and that none of the 22 instances have an extra script component.

Next I ran “Debug.Log(gameObject.name);” in CountBreakableBlocks() following the incrementation line. The output in the console was a little odd, as the Level Manager was printed out both first and last (I expected it to be either / or, not both).

Then I combed through the community, and the most common problem that came up was accidentally adding the “Level” (I called mine LevelManager) script to another object. To find which unintentional object got the script, it was suggested to log out all the objects getting counted using:
"Debug.Log(“Block script attached to: " + gameObject.name + " " + GetInstanceID(), gameObject);”

I’ve used this, but as you can see circled in my screenshot above, I get 22 objects being counted in the console rather than the 23 that appears in the Level Manager script in the Inspector.

As you can see from my script, I have initialized the breakableBlocks variable to zero.


Any insight would be greatly appreciated!

I’ve solved my problem: I included the CountBreakableBlocks() method call in the Start() method of my LevelManager() script, leading to the additional count. Once I deleted that line (and the entire Start() method from that script), I now have the expected 22 blocks.

I hope this helps someone else in the future.

1 Like

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

Privacy & Terms