Question by
Tannerhaley21 · Oct 18, 2016 at 05:05 AM ·
transformforeachmethods
Accessing transform child in foreach loop
foreach (Transform t in this.transform) {
totalWeight = totalWeight + t.CalculateWeight();
return totalWeight; }
It won't let me access CalculateWeight() which is a method of the child classes. What am i doing wrong?
Comment
Answer by SmomoGame · Oct 19, 2016 at 09:11 AM
If you want to get the function in your custom script, you must "Get" your script first.
foreach (Transform t in this.transform) {
totalWeight = totalWeight + t.GetComponent<YourScript>().CalculateWeight();
return totalWeight;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612094849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Looking through all the children doesn't work 1 Answer
For and Foreach not getting all child transform 0 Answers
how to gameobject timed destroy with effect 0 Answers
Dashing cube forward problem 1 Answer