- Home /
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?
Answer by LyanApps · Apr 03, 2013 at 02:22 PM
To obtain the run-time type of an expression, you can use the .NET Framework method GetType.
foodtype.GetType()
http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
Your answer
Follow this Question
Related Questions
what is the correct syntax for the creation of arrays? 2 Answers
if (!(apple is Vege)) then... 1 Answer
Passing Type variable to generic type: Variable not found. 1 Answer
Boo help: method not defined 4 Answers
Select a type from the inspector 0 Answers