- Home /
Question by
monkeyThunk · Mar 24, 2012 at 07:33 PM ·
classinheritanceinstancesubclassingjavascript class
Is Instance Of
Is there a way to determine if an object is an instance of a particular Class or one of it's super classes in UnityScript?
I'm only seeing an isInstanceOf function related to Android.
Comment
Answer by rutter · Mar 24, 2012 at 08:21 PM
The "`as`" operator attempts to cast a reference, and returns null if it fails.
function CheckComponent(Component foo)
{
var myCustomComponent = foo as MyCustomComponent;
if (myCustomComponent != null)
{
//do some stuff with your custom component
}
else
{
//the cast failed
}
}
Answer by cgarciae · Jan 15, 2014 at 08:21 PM
You are looking for the is
operator. Use:
SomeType obj = new SomeType();
if ( obj is SomeType ) {
//Do something
}
Answer by ledlamp · Oct 17, 2018 at 04:36 AM
Use instanceof
:
class _paddle {
}
class _paddle2 {
}
function Start(){
var player1Paddle = new _paddle2();
if (player1Paddle instanceof _paddle){
print("yes it is");
}
else{
print("no it is not");
}
}
http://wiki.unity3d.com/index.php/UnityScript_Keywords#instanceof