- Home /
Question by
xexex · Jun 13, 2012 at 12:17 PM ·
editorprefabcomponents
[Editor]How can I get components in non-instantiated prefab?
when i get components in non-instantiated prefab( in project-view),it always return null. Please help!
[MenuItem("Test/Test prefab")]
static void Test1()
{
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in selection)
{
Transform[] tans = ((GameObject)obj).GetComponentsInChildren<Transform>();
foreach (Transform tf in tans)
Debug.Log(tf);
}
}
Comment
Best Answer
Answer by Wolfram · Jun 13, 2012 at 02:59 PM
GetComponentsInChildren only returns active objects. Use GetComponentsInChildren<Transform>(true)
to also find inactive ones (which will include uninstantiated prefab components).
Also, I get a cast runtime error if there are non-GameObjects in your selection, so wrap your loop body in:
if(obj is GameObject){...}
In that case, feel free to mark this as answered, so it willl show up in green in the overview. :)
This is exactly the information I was looking for (2 years later). Thanks!!!