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?

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; 

}