- Home /
how to find all components of a game object
I was wondering if it is possible to obtain all the components that a game object has and output their names. I'm asking because I have an ImageTarget (Vuforia SDK) and it is being created Dynamically so I want to know which components are already part of the gameobject and which I will want to dynamically add.
Answer by Wolfram · Jun 27, 2012 at 10:02 PM
Why not just use GetComponents()?
var allComponents : Component[];
allComponents = gameObject.GetComponents (Component);
for (var component : Component in allComponents) {
Debug.Log(component.name);
}
Note this will include the Transform, too. To also get the Components of all children, use GetComponentsInChildren()
EDIT: Umh, wait...components don't have names, the loop will just print the GameObject's name over and over again. What exactly do you mean? Do you want to check their types, to see whether you need to add a particular type?
EDIT2: Example how to figure out which components are not attached yet, and attach them.
var allComponents : Component[];
allComponents = gameObject.GetComponents (Component);
// example: check for MeshRenderer and Material, replace/extend with the components you need
var foundMeshRenderer:bool=false;
var foundScriptOfType_MyScriptType:bool=false;
for (var component : Component in allComponents) {
if(component.GetType()==MeshRenderer)
foundMeshRenderer=true;
else if(component.GetType()==MyScriptType) // use an existing script's name here
foundScriptOfType_MyScriptType=true;
}
if(!foundMeshRenderer)
gameObject.AddComponent(MeshRenderer);
if(!foundScriptOfType_MyScriptType)
gameObject.AddComponent(MyScriptType);
There are more efficient and better ways (for example, using a List and/or array.Contains(), but I don't know the exact syntax for UnityScript, since we program in C# exclusively.
Yes this is exactly what i want, i want to be able to see what components are already attached to the gameObject currently found and which components need to be added
Well im using C# for unity so that i understand a bit more than javascript. But thanks. I followed your previous way and got the same except using if(gameObject.GetComponent() == null) { //add component code }
Any chance this code could be provided in C# ins$$anonymous$$d of JS? I'd like to debug and see all components of a loaded object but I'm not sure how to do that. Thanks!
@razcrux here you go :
bool found$$anonymous$$eshRenderer = false;
bool foundScriptOfType_$$anonymous$$yScriptType = false;
foreach (var component in gameObject.GetComponents<Component>()) {
if (component.GetType() == typeof($$anonymous$$eshRenderer)) {
found$$anonymous$$eshRenderer = true;
} else if (component.GetType() == typeof($$anonymous$$yScriptType)) {
foundScriptOfType_$$anonymous$$yScriptType = true;
}
}
if (!found$$anonymous$$eshRenderer) {
gameObject.AddComponent<$$anonymous$$eshRenderer>();
}
if (!foundScriptOfType_$$anonymous$$yScriptType) {
gameObject.AddComponent<$$anonymous$$yScriptType>();
}
Sorry if this is considered necroing, I believe that this contributes to the quality of the answer.
Answer by paver · Oct 10, 2013 at 10:34 PM
For some reason, when I put in component.GetType rather than component.name it gave me the names of my components:
var allComponents : Component[];
allComponents = gameObject.GetComponents (Component);
for (var component : Component in allComponents) {
Debug.Log(component.GetType());
}
that is a statement. Please do not post statements like that as an answer. use comments.