- Home /
Juggling with Type as a variable
I'll have to test for a lot of 'Types', and to avoid repeating a very long almost identical code, I'm trying to make it a function.
But I'm not sure how to pass 'Type' as a parameter?
This works / hardcoded:
foreach (MeshFilter one in checkthis.GetComponentsInChildren<MeshFilter>())
now, I'm trying to set 'Meshfilter' up to be a variable instead, like so:
foreach (thisType one in checkthis.GetComponentsInChildren<thisType>())
But I cannot write a proper variable for 'thisType', so it can be for example MeshFilter SkinnedMeskRenderer, sharedMaterial.. Is this even possible, how would you?
Thanks a bunch @Bunny83 ?
Answer by roojerry · Mar 10, 2017 at 10:09 PM
Could you use the other overload of GetComponentsInChildren that takes a type as an argument. Changing your foreach to:
foreach(var one in checkthis.GetComponentsInChildren(thisType))
Thanks - one might, but I do not appear to be able to write it right.
--
doButtons(checkthis, $$anonymous$$eshFilter);
--
void doButtons(GameObject checkthis, Type thisType)
{
foreach (var one in checkthis.GetComponentsInChildren(thisType))
{
//--
}
}
Error: UnityEngine.$$anonymous$$eshFilter' is a type' but a
variable' was expected
doButtons(checkthis, typeof($$anonymous$$eshFilter));
@roojerry thanks - but inserting '$$anonymous$$eshFilter' really does not make any sense: I am trying to figure out how to write '$$anonymous$$eshFilter' as a type-variable, so it can be for example $$anonymous$$eshFilter OR Skinned$$anonymous$$eskRenderer OR shared$$anonymous$$aterial..
A$$anonymous$$A a variable that can indicate the Type… am I not making any sense?
Your answer
Follow this Question
Related Questions
Unknown identifier: 'types'. js(38,61) 1 Answer
cannot convert UnityEngine.Gameobject to GameObject - What's the difference? 1 Answer
InvalidCastException with .net datatypes but not with UnityEngine.Object 1 Answer
How to make a custom type's contents modifiable in an array by inspector? 1 Answer
C# return question 1 Answer