Alternative to Find Object Type

Hi All,

In relation to the ball breaker game when Rick is finding the object type - there is another way which worked for me too. You can use transform.childcount, which can be used my attaching it to your ‘Blocks’ group and simply when the number of blocks in there reach 0, the code loads the next scene.

using UnityEngine;
using UnityEngine.SceneManagement;

public class ProgressionScript : MonoBehaviour
{
    int nextLevel;
    int numOfBlocks;

    private void Start()
    {
        int current_scene = SceneManager.GetActiveScene().buildIndex;
        nextLevel = current_scene + 1;
    }
    private void Update()
    {
        numOfBlocks = transform.childCount;
        if (numOfBlocks == 0)
        {
            SceneManager.LoadScene(nextLevel);
        }
    }

}

1 Like

This is a great solution but there’s a small problem that you should keep in mind. The less conditions you add to your code to work properly, the better.

Rick’s solution doesn’t actually have any condition.
Your’s has one, blocks should be parented to the object with that script.

This forces you to check all the levels’ hierarchy throughly in case you moved a block out of place, this might occur because you’ll be manipulating this objects in every level. In case of Rick’s solution you won’t have to do that since the script finds the objects on its own. This might sound like a very dumb thing with a project of this size, and it might well be, but when working with a bigger project you’ll have a hard time, so keep that in mind for future projects.

One way to solve this is by parenting the blocks via script in the start method, which is fairly easy to do.

1 Like

Thanks for the feedback! I might change my code to Rick’s.

Privacy & Terms