I have a three classes: Entity, Student and Teacher.
All objects are saved in Entity array.
I need to verify class of Entity[i]
item but when I try to virify I recieve an warning. Program is stopped and nothing goes. What to do?
class Entity {
string param0;
}
class Student : Entity {
string param1;
//consturctor...
}
class Teacher : Entity {
class string param2;
//consturctor...
}
Entity[] entities = new Entity[5];
entities[0] = new Student("some string1");
entities[1] = new Teacher("some string2");
...
...
var es = entities[i] as Student;
if (es.param1 != null) //here throw nullReferenceException
Debug.Log(es.param1);
else
Debug.log(es.param2);
What i do incorect? How i can verify object class correctly?