unity3d - Unity/C# Find object which have generic component -
i totally exhausted @ drag , drop components in inspector.
what want find every object have component had chosen t
void getallgameobjectwhichhave<t>(ref int cnt, gameobject[] f_array, gameobject transformforsearch) { foreach (transform trans in transformforsearch.transform) { getallgameobjectwhichhave<t>(ref cnt, f_array, trans.gameobject); t temp = trans.gameobject.getcomponent<t>(); if (temp != null && cnt < f_array.length) { f_array[cnt++] = trans.gameobject; } } }
and used this
//onawake() getallgameobjectwhichhave<rigidbody2d>(ref zeroint, enougharray, tophierarchyobj);
this recursive function gets every gameobject , save them @ f_array. cannot filter , 'temp' not null.
t temp = trans.gameobject.getcomponent() doesn't seem working meant.
did misunderstand something?
just use findobjectsoftype<t>()
. (yep, generic variant exists) array of components of type. there can whatever like.
or, in case restrict components found within children of gameobject, can use getcomponentsinchildren<t>()
. array of components of type within children of object.
Comments
Post a Comment