This is really stressing me out. I understand what I’m supposed to do - check the parent for children and if it has 1 right before the last one is destroyed, then the next level loads.
But I’m confused about where to put this GetComponentsInChildren code. I take it it goes simply in the script I attach to each enemy. But that way, how does it know to go to the parent to check for children?
I don’t understand what the parameter “HingeJoint” is supposed to be. I believe that is the parent that is checked, so I tried using the name of the parent and the tag of the parent, but it didn’t work.
I don’t understand those pages on the support site. They just show code that doesn’t make sense to me, and I like to know what it means and how it is used.
I don’t understand why you are instantiating an array of type SmallTurret (script name). And it looks like the parameter of GetComponentsInChildren should be the parent (Enemies). I’m so confused.
You have to tell your code everything. If you’re worried that a Turret won’t know which Enemy to report to, -tell- it which Enemy to report to.
Each Enemy should be capable of finding its own Turrets and telling them that it is their Enemy; then they can easily tell it when they blow up and it can easily keep track of how many are remaining.
gameObject.GetComponentsInChildren(X) will return :
gameObject’s X component
all X components in all children of gameObject
Enemy.js
// How many turrets remaining
private var turretCount : int = 0;
function Start() {
...
// Initialize our turretCount
// Find all Turret children we have and count them
// GetComponentsInChildren return an Array filled with Turret objects.
var turrets = GetComponentsInChildren(Turret).length;
for ( turret : Turret in turrets ) {
turret.SetEnemy( this );
}
turretCount = turrets.length;
...
}
function TurretDestroyed() {
turretCount -= 1;
if ( turretCount == 0 ) IAmDestroyed();
}
Turret.js
private var myEnemy : Enemy;
function SetEnemy( enemy : Enemy ) {
myEnemy = enemy;
}
function Destroyed() {
myEnemy.TurretDestroyed();
}