- Home /
Get name of inherited type?
Hi folks,
how can I access the name of a type inherited from MonoBehaviour?
I've got a code block that looks like this:
var objCollection : List.<MyBehaviour>;
for(var obj in objCollection)
{
Debug.Log("Type: " + (typeof obj));
}
where MyBehaviour inherits from MonoBehaviour. The collection however contains elements of other types that themselves inherit from MyBehaviour. I'd like to get the name of the "deepest" type in that inheritance tree. Debug.Log only writes "Type: MyBehaviour" to the log.
I'd like to invoke specific actions depending on the type of 'obj'.
Any idea?
Thanks a lot!
Answer by Bunny83 · Mar 23, 2012 at 11:22 AM
Sure, the typeof operator just determines the type of the variable, which is of course MyBehaviour since that's the type stored in the List. To get the dynamic type you have to use obj.GetType() which returns the System.Type object for the object that is referenced by this variable.
Well, unfortunately that still returns $$anonymous$$yBehaviour.
//edit: my bad! It's in fact been $$anonymous$$yBehaviour. The "deepest" type is provided in a different way. It's not inherited.
Thanks anyway! I guess your answer would have worked if it had been inheritance.
Your answer
Follow this Question
Related Questions
Building a Collection of Subclasses and Modifying Their Public Members from the Inspector 0 Answers
how to apply same fields for objects with different script but inherit same class Unity C# 1 Answer
Get all inherited classes of an abstract class - Editor Scripting 1 Answer
A smarter Way to get the Type of SerializedProperty 6 Answers