The question is answered, right answer was accepted
Checking for a class type issue
Hello guys, i have a list with different object types. And i want to take some from list. Im stuck in checking for needed type.
public Type GetRandomSkill<Type>()
{
List<Type> skills = new List<Type>();
Debug.Log("checking for: " +typeof(Type));
for (int i = 0; i < enemy_Skills.Length; i++)
{
Debug.Log("current: "+enemy_Skills[i].GetType());
if (enemy_Skills[i].GetType() == typeof(Type))
{
skills.Add(skills[i]);
Debug.Log("added");
}
}
return skills[Random.Range(0,skills.Count)];
}
As you can see from console, checking type and needed type are equal on second iteration, however if brackets dont work. I know issue is in this line if (enemy_Skills[i].GetType() == typeof(Type))
but how to fix it?
Answer by FortisVenaliter · Feb 09, 2017 at 06:56 PM
You're checking whether the object is the Type type. You probably want typeof(Skill) or something like that.
But im using this method like GetRandomSkill<Skill>()
So Type is Skill
Follow this Question
Related Questions
How to add a Type to a Class in Javascript? 1 Answer
Why I Cannot cast from source type to destination type 0 Answers
Inheritance and type casting problems 0 Answers
generics c# 1 Answer