Why `GetComponentInChildren()`?

(I have split grabbing the slot and then accessing the prefab, but it should be the same as the course’s script)

To get the InventorySlot we do this:

InventorySlot invSlot = transform.GetChild(activeSlotIndexNum).GetComponentInChildren<InventorySlot>();

and on this we do

GameObject weaponToSpawn = invSlot.GetWeaponInfo()?.weaponPrefab;

to get the prefab.

Now, in the hierarchy this is what we have (minus me having them prefabbed already):

image

So, shouldn’t the GetChild() of the current slot index already give us the InventorySlot which is exactly the object that has the InventorySlot component on it?

So why should we have to do a GetComponentInchildren<>() to grab it, then?

I would think GetComponent<InventorySlot>() would be perfectly fine…

For some reason I also can’t even do that first swing down of the sword. I can run around and hit things though, as the weapon’s collider is active when the weapon gets spawned…

I hope that will be cleared when finishing the second half of this part and all the interactions between player and sword have been rebuild…

Hi heckert,

You are absolutely correct and when we do the null check before we dont need to be checking the children either as GetComponent would suffice. I’ve given Stephen the heads up on this.

I’m not sure why your first swing is not working as it should be working at the end of this lecture.
I am guessing it worked before we switched weapons so if you could post your code and we can take a look as a second pair of eyes can often spot issues (I have to get my wife to look sometimes although most of the time her reply is “Did you forget a semi colon” >.< Shes not a coder!)

Let me know on the sword issue.

@Marc_Carlyon I attached a checkout of my current project state on the itch page.

I dont think i have seen your itch page or if i did my memory is failing me.
If you can let me know where i can try to get a look in before the weekend :slight_smile:

I made a build and uploaded it to itch.io :wink:

@Marc_Carlyon thank you for pointing me at where the breaking point of my script was.

Adding one line to grab the weaponCollider from the scene “fixes” it (so I can do one swing of attack, just like the state at the end of the lesson).

            weaponCollider = PlayerController.Instance.transform.gameObject.GetComponentInChildren<DamageSource>().transform;
            weaponCollider.gameObject.SetActive(false);

But, traversing through the object hierarchy like that is somewhat brittle since it would break on any seemingly minor re-arrangement of the object hierarchy, and the following lesson will surely get to a much better solution that will survive more than one swing… :slight_smile:

I still wonder why the projects would deviate in a way that would make it work in the course and fail for me…

1 Like

We’ve added an edit to the video to explain although we can just use GetComponent and probably should GetComponentInChildren will work as it checks the top level and down the heirarchy until it finds it or reaches the end.
At the end of the day GetComponent is probably more efficient though

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms