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.
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.
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
}
}
You are looking for the is
operator. Use:
SomeType obj = new SomeType();
if ( obj is SomeType ) {
//Do something
}
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