Check scene for Tagged object

Hello, im trying to check my scene for tagged object, if there is no object on the scene i want to go to level 2:

var bricks : GameObject[];
 
function Awake()
{
    bricks = GameObject.FindGameObjectsWithTag("Brick");
}
 
function Update ()
{
    for (var bricki : GameObject in bricks)
    {
        if(bricki /* What do i need here to check if the stage is empty? */)
        {
        	print("There is still bricks on scene");
        }
        else
        {
        	Application.LoadLevel(2);
        }
    }
}

Or is there another way to do this :o NEW TO SCRIPTING :wink:

Bump!

How do you destroy your bricks? Because thats relevant to how your check if they exist.

The way you want to do it is probably something like this:

var doBricksExist : bool = false;
for (var bricki : GameObject in bricks)
{
        if( bricki != null )
        {
            doBricksExists = true;
            break; //stops the for loop, because if you have found one, you don't need to check the others
        }

        //Dont put this here, because it would go into "else" if even just one brick is empty
        //else
        //{
        //    Application.LoadLevel(2);
        //}
}

if( doBricksExist == false )
{
    Application.LoadLevel(2);
}

However, while this would work, I would not recommend checking the scene in every Update function. Rather I would check each time you Destroy one brick if there are others left. This way you don’t have to do it as often. It would be the same as above, just in some function thats executed when you destroy a brick

Umm at line 2 where it says bricks, do i need to put the tag name there? or the name of the brick?
If its not the tag name, is it posible make it to that?

Thanks for the reply

You do exactly the same you did in the Awake function, that was perfectly fine.

I tryed the script like this:

var doBricksExist : boolean = false;

var bricks : GameObject[];

 

function Awake()

{

    bricks = GameObject.FindGameObjectsWithTag("Brick");

}


function Update()
{
for (var bricki : GameObject in bricks)

{

        if( bricki != null )

        {

            doBricksExist = true;
            print("doexist");

            break; //stops the for loop, because if you have found one, you don't need to check the others

        }

 

        //Dont put this here, because it would go into "else" if even just one brick is empty

        //else

        //{

        //    Application.LoadLevel(2);

        //}

}

 

if( doBricksExist == false )

{

    Application.LoadLevel(1);

}
}

It gives me no errors but when all the bricks are broken it dosent load next level?

Well you didn’t show me how you destroy your bricks. So I was just guessing. If you tell me, I can help better

This is the code on the brick that destroys it if the ball hits it:

function OnCollisionEnter(col : Collision)
{
	if(col.collider.name == "Ball")
	{
		Destroy(gameObject);
	}
	else
	{
		print("Not the ball");
	}

}

Ok so I would recommend to just do all of this in the OnCollisionEnter function. This way you don’t have to check it every frame and you have one component less you have to worry about :wink:

function AreAllBlocksDestroyed() : boolean
{
    var bricks : GameObject[] = GameObject.FindGameObjectsWithTag("Brick");
    
    return bricks.Length == 0;
}

function OnCollisionEnter(col : Collision)
{    
    if(col.collider.name == "Ball")
    {
        Destroy(gameObject);

        if( AreAllBlocksDestroyed() == true )
        {
            Application.LoadLevel(1);
        }
    }
    else
    {
        print("Not the ball");
    }
}

I got different types of bricks, like some needs to be hit 3 times to get destroyed, so it would be easier to make a seperat script for this i guess :o?

Yeah, you’re right. I guess I was just trying to impose my way of building it onto you, sorry about that :slight_smile:

Well you could just use the AreAllBlocksDestroyed function in your LevelChecker script. A quick note on that: While I don’t think it will be relevant for your game, searching for all the bricks each frame can get expensive depending on the amount of bricks. So if this part of your game gets slow, you should try to find another way to do it (like trigger the check only when a brick is destroyed). But yeah again, I don’t think you’ll have a problem with that so don’t worry about it

Thank you very much for healping me out :slight_smile: