Find which class has extended the base class?

Just say I have a Class Car().
And I’ve made several extensions…
BlueCar : Car
RedCar : Car

In my script, I collect all Cars into an array through GetComponent(typeof(Car)).

If I’m iterating through that array, how can I tell if the object was a BlueCar or a RedCar?

cheers!

You didn’t say what language you’re scripting in… Assuming C#:

if (obj is BlueCar) ...

will tell you if it’s a BlueCar or anything that sub-classes BlueCar. If you need to test specifically for is BlueCar but a sub-class of BlueCar:

if (obj.GetType() == typeof(BlueCar)) ...

I’m No Class master. In the inventory topics in the forum there are a few cool ideas for creating classes and getting them into arrays. On a script for each car something like this

public var carName:String;
public var carType:String;
public var carColor:String;
public var carIcon:Texture;

var car = new caritem();
car.carObject = gameObject;
car.carNameID = carName;
car.carTypeID = carType;
car.carColorID = carColor;
car.carIconID = carIcon;

This way the class will be full of deails for each car. Once you have the car in the array you can view details from inside the array. :smile:

laurie is spot on. If you want to dive into it further, urgrund, there’s a whole suite of type reflection available in C#: http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx

Black Mantis, I’m guessing this is a workaround for UnityScript? I thought it essentially had the same libraries as C#?