- Home /
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
@navadeep2011 thanks, sorry I made a mistake I edited my post, but I also tried with your code, and it says "Cannot infer generic arguments for method 'UnityEngine.GameObject.GetComponents.()'. Provide stronger type information through arguments, or explicitly state the generic arguments." When I provide the type "GameObject", the error is the one in my post... any idea?
Answer by whydoidoit · Apr 26, 2013 at 08:03 AM
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>();
Answer by KiraSensei · Apr 26, 2013 at 07:50 AM
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");
}
@$$anonymous$$iraSensei thanks a lot $$anonymous$$iraSensei
Answer by Ailaxgt · Apr 26, 2013 at 07:50 AM
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. :) 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();
}
}
Your answer
Follow this Question
Related Questions
How do I assign a script to an instantiated prefab? 2 Answers
Counter for sprite under script from GameObject prefab 0 Answers
Get unity to recognize prefab in C# 2 Answers
How to Play 3D Sound and Keep Script on Multiple Prefabs after Destroying a gameObject? 0 Answers
GetComponent vs AddComponent 3 Answers