- Home /
(C#) How do I run something in my child's script?
I've tried a few things but I can't seem to figure it out
this:
hex = transform.FindChild("Hex").GetComponent<Hex>();
Doesn't seem to do it either, am I doing something wrong?
I want to run something in my child's script, how do I access it and run it?
Answer by MrSoad · Oct 14, 2014 at 10:15 AM
Try this, I've split it into two lines to make it easier to read.
oChild_Object = transform.parent.FindChild("Child_Object_Name").gameObject;
oChild_Object_Script = oChild_Object.GetComponent(Your_Child_Object_Script_Name);
No quotes on the script name, just the name exactly as is exculding the ".js" . Hope this helps
Oh, declare your script variable like this :
private var oChild_Object_Script : Your_Child_Object_Script_Name;
I'm sorry I use C# and trying to convert this it doesn't work.
Answer by tanoshimi · Oct 14, 2014 at 10:18 AM
FindChild
is not a method of Transform (I think it may have been some time ago, but it's certainly not in the method reference now.). Try Find
instead.
Otherwise, if the GO to which this script is attached has a child called "Hex", and that child has a script attached called Hex, this should work. If you only have one child object with a Hex script on it, you could also try
hex = GetComponentInChildren<Hex>();
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
find ALL children of a parent 1 Answer
How To Get List of Child Game Objects 14 Answers
accessing all of a colliders parents children 1 Answer
Finding child problems.. 1 Answer