syntax to define a variable of type "type"?

the Animal class has two children classes: Dog, and Cat. i am now writing a component for pet food, and i want to expose a variable in the unity inspector that specifies whether this pet food is for Dog or for Cat. i’m trying to write something like:

public Type foodtype;

in the pet food component, and then check if the dog can eat it by:

if (foodtype == typeof(Dog))
   // eat food.

but i can’t figure out the proper syntax for this. would someone kindly lend a hand?

1 Answer

1

To obtain the run-time type of an expression, you can use the .NET Framework method GetType.

foodtype.GetType()