- Home /
Type 'Force' does not contain a definition for 'Operation'
Ok. So I have a problem with communication between two scripts.
First I'm sending you the part of my first script below:
Glass.GetComponentsInChildren<Force>().Operation(col.gameObject.transform);
In this script I have a variable of type GameObject called "Glass". I'm using it to call my functions from other script which are attached to Glass's childrens. These scripts are called "Force" and they have a "Operation" method which have a argument of type Transform.
Below my function in script "Force":
public void Operation(Transform Direction)
{
Debug.Log ("Function called");
float Force = Random.Range(5f, 10f);
rigidbody.AddForce(Direction.forward * Force, ForceMode.Impulse);
}
All is going well but when I'm playing the game I have a warning:
Type 'Force[]' does not contain a definition for 'Operation' and no extension method 'Operation' of type 'Force[]' could be found (....).
Answer by NickP_2 · Jun 01, 2014 at 01:29 PM
GetComponentsInChildren returns an array of components, while GetComponentInChildren (notice the "s" is gone) jsut returns the first found component. You're trying to execute a function on a list now! ;)
if you want each child with a Force component to execute the function:
For(int i = 0; i < Glass.GetComponentsInChildren().Length; i++)
{
Glass.GetComponentsInChildren<Force>()[i].Operation(col.gameObject.transform);
}
just one:
Glass.GetComponentInChildren<Force>().Operation(col.gameObject.transform);
didn't check for compile errors, hope this puppy runs!