Having trouble understanding how childCount works?

Hello all,

I had a lot of problems with the direction of previous lectures with regards to collision detection and didn’t use the code presented in those lectures, however, it seems we are back on point now.

But, I am having trouble understanding how the childCount works and how childCount ever reaches zero. Firstly, the code works fine, I tried it out, I just don’t understand how.

I tried this line in the bottom of the Update loop:

print ("Num Children: "+tranform.childCount);

and no matter how many enemies were destroyed it always came up the the same number. So I am not seeing how it works. Whenever I am presented with code I don’t know I come up with little things like this to prove that I am understanding what it does and getting the results I expects - but, In this case I am not getting it.

To anyone who can help here is a big thankyou in advance!

Regards,
Vaughan.

hmmm, I just came up with some example code that shows what’s going on in relation to the code, but, has me confused as to what going on in Unity.

	public void TestCount()
        {
          int counter=0;

	  foreach(Transform looper in transform)
	  {
		counter++;
		if (looper.childCount == 0)
			print("Child: "+ counter + " is dead");
	  }

    }

Which to me looks like we are checking for a child object on a child object. That is the enemy position transform. Like we have the enemy Formation transform (the parent) then the child components, the position, then in this child component, we are looking for the transform where our enemy is instantiated. And then we test this condition to see if that components child is zero before we consider that this enemy is dead.

Which means … my mind is melting :scream:

Someone please explain!!!

1 Like

Hello @Vaughan_MacEgan, how are you?
This happens because the child of the child (position) of the formation is the enemy that is being instantiated. This code checks if there is a enemy (child) inside the position (child of the formation), and case there is not, it means that the enemy was killed. If there was a enemy childed to that position the childcount would return 1, thus meaning that this position have an alive child inside it. The transform only exists if there is an gameObject, when you destroy the gameObject, the transform os also destroyed.

3 Likes

Aha, I think I’ve got it! We are looping through the child objects to see if they have any child objects - if the child objects transform (eg sprite) has been destroyed it equals zero. Not as I thought, just counting the number child objects of the parent - d’oh! I still think it’s a bit weird, but, I can get my head around it. And I think I should be able to reuse this “childCount” in future projects, providing I structure it correctly.

Thankyou for your input, much appreciated :slight_smile:

Regards,
Vaughan.

1 Like

Yes!

The Position gameObject within the formation is just the object that controls the position within the formation, it isn’t the enemy, the enemy is the GameObject instantiated inside it.

Thanks so much for your input. I don’t think I would’ve been able to wrap my head around this concept without your help :sunglasses:.

Regards,
Vaughan.

1 Like

Im glad for being able to help, let me know if you need something else :slight_smile:

How good are you at changing weather patterns :stuck_out_tongue:. I could really do with a couple of days under 30 degrees Celsius :sunglasses:.

1 Like

void changeCelcius(float degrees)
{
Celcius += degrees;
}

void Start()
{
changeCelcius(-30);
}

Although you have to use GODstudio IDE

4 Likes

Actually would be better to use an mathf.clamp

1 Like

Rolling on the floor laughing my ass off :laughing:.

1 Like