- Home /
Check if class belongs to part of tree (extended from)
In javascript:
if class B extends class A
and variable C = B
how can I check if C is an extension of A?
Answer by Bunny83 · May 05, 2013 at 03:32 PM
I guess you use UnityScript (since you talk about "extend"). Usually there's the "is"-operator. It should work like this:
if (C is classB)
So i guess the variable C is of type classA and hold a reference to an instance of classB. In this case the above statement should return true.
I'm not sure if the is-operator exists in UnityScript, but since they adapted most JS-unusual stuff from C# it should work ;)
You cannot use 'is' in JS/US. That's C# only. That's why I'm asking :)
The only way I know is: if(C.GetType() == B)
This will return true, but it will return false if checking against A.
For some reasons, it seems the members from the System.Type namespace have been simply...forgotten.
For instance your issue could easily be solved with IsSubclassOf(BaseClass) but it is nowhere to be found...
Well, using refletion isn't really a good way. You can still use the as-cast. It will return null if it can't be casted to this type. In addition you might want to check for null since the variable could be null before the cast.
if ((c as classB) != null)
It's not that nice but should work as well.
True. Still what happened to all those reflection methods?
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Arrays In Classes? 2 Answers
Accessing classes class from other script 2 Answers
Benefit of nested classes? 1 Answer