Search Unity

How can I categorize?

Discussion in 'Scripting' started by beybi123, Aug 18, 2017.

  1. beybi123

    beybi123

    Joined:
    Oct 26, 2016
    Posts:
    25
    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.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There are multiple methods.

    Tags, FindObjectOfType, is, GetComponent.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    use an enum called elements with {water, fire, etc.}

    maybe tell us the use of the categories?
     
  4. kaskiU

    kaskiU

    Joined:
    Aug 6, 2013
    Posts:
    50
    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):
    1. public abstract class Elements
    2.     {
    3.         public enum ElementTypes
    4.         {
    5.             Fire,
    6.             Water
    7.         }
    8.  
    9.         public abstract ElementTypes ElementType { get; }
    10.     }
    11.  
    12.     public class Fire : Elements
    13.     {
    14.         // C# 6.0 Expression Bodied Property
    15.         public override ElementTypes ElementType => ElementTypes.Fire;
    16.     }
    17.  
    18.     public class Water : Elements
    19.     {
    20.         // Same function as with Fire but without C# 6.0 support
    21.         public override ElementTypes ElementType
    22.         {
    23.             get { return ElementTypes.Water; }
    24.         }
    25.     }
    Another way is to use is keyword but if you have many elements then you will have way too many if statements.
    Code (CSharp):
    1. public void Test1(Elements element)
    2.     {      
    3.         if (element is Fire)
    4.         {
    5.  
    6.         }
    7.     }
    Following is untested but it should work:
    Code (CSharp):
    1. public void Test2(Elements element)
    2.     {
    3.         switch (element.GetType().Name)
    4.         {
    5.             case nameof(Fire):
    6.                 break;
    7.             case nameof(Water):
    8.                 break;
    9.         }
    10.     }
    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):
    1. switch (element.GetType().Name)
    2.         {
    3.             case "Fire":
    4.                 break;
    5.             case "Water":
    6.                 break;
    7.         }