How do I access a component, such as a script, that’s in the grandchild of the game object with the script I’m working with? Here is my specific situation: I have a “Player” game object with a child game object called “Weapons”, and that has children, one of which is a “Sword” game object. The player has a “PlayerScript” which I’m trying to access “SwordScript” in the Sword game object. Here is a visual representation:
The way I am trying to do this is as follows (this code comes from the “PlayerScript” script attached to the Player game object:
SwordScript _swordscript = this.GetComponentInChildren<Transform>().gameObject.GetComponentInChildren<SwordScript>();
The first GetComponentInChildren() should be getting the Transform that is in the Weapons gameobject, and that should be getting the SwordScript in its child.
I realize this may not be a very intuitive way to access the grand child (if I’m even remotely close to this code working), so any suggestions on how to better access the script in the grand child I would really appreciate.