Checking if all objects are boolean=true to finish a level.

I have 4 different prefabs, that each will be duplicated in any given level various times, and each has its condition to be “activated”.

When all prefabs are activated (each in their own way), the level is finished. This sounds like a simple problem, but can’t get my head around on finding a solution to do this in a way that I can repeat the code in every new level I create.

I was trying to use GameObject.FindObjectsWithTag, but then don’t know how to check if the boolean on all of them is true.

I’m a noob in coding, so any help is greatly appreciate! :slight_smile: (And I’m guessing there is a extremely easy way to do it :sweat_smile: )

Static vars!
<3
They let you keep exactly one copy of a variable and access it easily!

GameHandler.js

static var objectsToDo : int;

function Awake() {
 objectsToDo = GameObject.FindWithTag("Tag").length;
}
static function DidObject( twd : ThingWhichDoes ) {
 objectsToDo -= 1;
 if ( objectsToDo <= 0 ) {
   NextLevel();
 }
}
ThingWhichDoes.js

function Do() {
 GameHandler.DidObject( this );
}

Hey Vicenti

That was my idea (more or less) but didn’t know how to script it. It worked! Thanks a lot!

Just a small correction, on your code, you wrote

objectsToDo = GameObject.FindWithTag("Tag").length;

And it should be

objectsToDo = GameObject.FindGameObjectsWithTag("Tag").length;

Thanks! :smile:

Hi Vicenti

Another one that you might be able to help.

Further to that, on the same lines as the previous problem, I need to create sub-groups, where I might have 8 objects in total, but I need to trigger an event when 4 specific objects are then “activated”.

I can’t use tags as these are already in use by the previous solution and I can’t tag a single object with more than one tag.

Thanks again.

What I would do is

GameManager.js

static var groupCount : int[];

static function AddGroupMember( group : int ) {
 if ( group >= groupCount.length ) {
  var x = new Array( groupCount );
  while ( x.length <= group ) {
   x.Add(0);
  }
  groupCount = x.ToBuiltin(int);
 }
 groupCount[ group ] += 1;
}
static function GroupMemberDestroyed( group : int ) {
 groupCount[ group ] -= 1;
 if ( groupCount[group] <= 0 ) GroupDestroyed( group );
}

static function GroupDestroyed( group : int ) {
 // stuff
}

and

GroupedObject.js

var myGroup : int;

function Start() {
 GameManager.AddGroupmember( myGroup );
}
function Destroyed() {
 GameManger.GroupMemberLost( myGroup ); // or "this" if you need the groupedobject info
}

There’s probably some efficientizing to do in there too. :slight_smile:

Hey Vicenti

Thanks for your idea.

Before I saw your reply, I had an idea of doing more or less the same as your first suggestion, but tagging a child object and activating it when the objects was visible to countdown.

It worked.

Thanks anyway for your suggestion.

Cheers

PS.: Great Forum! Great people helping! :smile: