- Home /
Question by
GerryM · Apr 27, 2012 at 02:44 PM ·
c#gameobjectcomponents
Check for empty gameobject in script?
Is there a way to check in a script if a gameobject is empty?
Checking for all documented components would not asure there is no custom script...
Any ideas?
Thanks
G
Comment
I think you might want to described what you mean by an "empty gameobject"?
"empty gameobject" = go that does not contain any components apart from the default transfer component; i.e. what you get when you choose GameObject/Create Empty from the menu.
Best Answer
Answer by Statement · Apr 27, 2012 at 03:04 PM
You could probably use GameObject.GetComponentsInChildren or GameObject.GetComponents and check if there are any other components than Transform.
var components = targetGameObject.GetComponents<Component>();
// check components...
Then check the length of "components"- if it's greater than 1, return false, if it equals 1 and components[0] is a Transform, return true.