Hi, My question is kinda easy.I want to be able to categorize my objects.To be more clear,let's say, I have 3 classes Fire(1) and Water(2) inherited from Elements(3).How can I easily pick which gameobject is Fire or Water via scripts?I tried defining booleans for each element but as the number of elements increase ,well..it sucks. Thanks in advance.
What you mean by that? Do you have a unknown type of an object and you want to determine which of the elements it is? If so then.. If your base class is abstract then you can create an enum in it and assign values for each element. Like this: Code (CSharp): public abstract class Elements { public enum ElementTypes { Fire, Water } public abstract ElementTypes ElementType { get; } } public class Fire : Elements { // C# 6.0 Expression Bodied Property public override ElementTypes ElementType => ElementTypes.Fire; } public class Water : Elements { // Same function as with Fire but without C# 6.0 support public override ElementTypes ElementType { get { return ElementTypes.Water; } } } Another way is to use is keyword but if you have many elements then you will have way too many if statements. Code (CSharp): public void Test1(Elements element) { if (element is Fire) { } } Following is untested but it should work: Code (CSharp): public void Test2(Elements element) { switch (element.GetType().Name) { case nameof(Fire): break; case nameof(Water): break; } } If I rememeber correctly then nameof() is yet again C# 6.0 feature. I guess you can replace it with a string but that might provide problems in future (for example in refactoring). Code (CSharp): switch (element.GetType().Name) { case "Fire": break; case "Water": break; }