Question by
twinplay · Dec 30, 2017 at 01:37 PM ·
inheritancesubclasssubclassing
Getting the type of the subclass
I'm experimenting with Inheritance at the moment and I want to detect what kind of Subclass my GameObject is. I already found this out:
(fruit as Apple).Speak();
and:
fruit.GetType();
and:
if (fruit is Apple)
{
}
else if (fruit is Banana)
{
}
else
{
}
But is there a shorter way to find the Type? And can I save the Type in my fruit
variable somehow so that I don't have to check again if I want to use another Method? Maybe you can save the Type somehow and do something like this:
Type fruitType = fruit.GetType();
(fruit as fruitType).Speak();
Comment
Your answer
Follow this Question
Related Questions
Check if script is a particular Class 1 Answer
Access to a function from a parent class who have a GetComponent 1 Answer
Define attacks to choose from 1 Answer
How to inherit a boolean 2 Answers
Polymorphism on member attribute 1 Answer