Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Store child class in parent list and then find the specific child in the list

Discussion in 'Scripting' started by psykick1, Jun 29, 2020.

  1. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hello,

    I have 2 child classes which derived from parent class:

    Parent class:
    Animal

    Child Class 1 :
    Dog

    Child Class 2:
    Bird


    I'm storing instance of Dog and Cat inside List<Animal> animals = new List<Animal>();

    And then want to loop inside animal list to find all instances of dog or cat.

    But actually i'm getting error - I know why it happend's, it because when I search for dogs is also loop in cat elements
    but dog is not cat , so I'm getting cast problem.

    I'm very sure that there is smarter way to do it.

    I attached the my code:

    Code (CSharp):
    1. public class programClass
    2.  
    3. {
    4.     public Animal insTClass;
    5.     public List<Animal> components;
    6.  
    7.  
    8.     public void CreateNewList()
    9.     {
    10.         components = new List<Animal>();
    11.      
    12.  
    13.     }
    14.     public void instanceMethod<T>() where T : Animal, new()  // Im storing Cat and dogs from another script(storeManager).
    15.     {
    16.         insTClass = new T();
    17.         components.Add(insTClass);
    18.         insTClass.parentID = insTClass.ToString();
    19.     }
    20.  
    21.     public T searchInList<T>() where T : Animal  // search for cat inside animal
    22.     {
    23.         foreach (T instclass in components)   // Here im getting the error.
    24.         {
    25.             if (instclass.parentID != instclass.ToString())
    26.             {
    27.                 continue;
    28.             }
    29.             return instclass;
    30.         }
    31.         return null;
    32.     }
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    You shouldn't rely on
    ToString
    to do type testing. Use the
    is
    and
    as
    operators instead.

    But in your case, the cast error happens before that.
    foreach
    will always iterate over all elements, so you need to use
    Animal
    there and then check the type later. You can use
    VAR is TYPE NEW_NAME
    to check the type and assign it to a variable if the type matches in a single line.

    Code (CSharp):
    1. public T searchInList<T>() where T : Animal  // search for cat inside animal
    2. {
    3.     foreach (Animal comp in components)   // Here im getting the error.
    4.     {
    5.         if (comp is T instclass)
    6.         {
    7.             return instclass;
    8.         }
    9.     }
    10.     return null;
    11. }
     
  3. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Thanks,
    Great . work smooth..