GetComponents error with GameObject?

Hi,

I don’t understand this error :

BCE0149: The type ‘UnityEngine.GameObject’ must derive from ‘UnityEngine.Component’ in order to substitute the generic parameter ‘T’ in ‘UnityEngine.GameObject.GetComponents.()’.

with this code in unityscript :

just1Explode = Instantiate(Resources.Load("explode1"), vec, Quaternion.identity);

var gos : GameObject[];
//gos = just1Explode.GetComponents.();
    gos = just1Explode.GetComponents.<GameObject>();
for (var jo : GameObject in gos) {
  	Debug.Log("h");
}

I have got a prefab with several gameobject inside, each with a rigidbody, and I would like to add a different impulse to each of them to create an explosion.

Thanks for your help

The problem is with the line.

gos = just1Explode.GetComponents.<GameObject>();

You are trying to get Components of type GameOject but GameObject is not a Component. I hope the following code helps. :slight_smile: My JS is close to non-existant.

var gos : MyExplosingScript[];
    gos = just1Explode.GetComponents.<MyExplosingScript>();
	for (var jo : MyExplosingScript in gos) {
		Debug.Log("h");
		jo.StartMyAwesomeExplosion();
		}
    }

GetComponents returns a list of Components.

just1Explode = Instantiate(Resources.Load("explode1"), vec, Quaternion.identity);
 
var gos : Component[];
gos = just1Explode.GetComponents();

for (var jo : Component in gos) {
    Debug.Log("h");
}

EDIT : I just saw that you said that just1Explode has children, so you have to loop on its children and then search the right component in them. So the right method to use should be GetComponentsInChildren()

just1Explode = Instantiate(Resources.Load("explode1"), vec, Quaternion.identity);
     
    var gos : Component[];
    gos = just1Explode.GetComponentsInChildren<YourComponent>();
    
    for (var jo : Component in gos) {
        Debug.Log("h");
    }

GameObject isn’t a component and cannot be got that way - in any case, all of the components share the game object you are calling it on!

What you actually want to do to get all of the rigidbodies so you can add the force is:

  var rigidbodies = just1Explote.GetComponentsInChildren.<Rigidbody>();