Search Unity

How find out a member in list that the member of the member match?

Discussion in 'Scripting' started by Xhitman, Jan 12, 2019.

  1. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    268
    I have to admit this question look quite stupid...

    First, I have this:

    Code (CSharp):
    1. ublic class Trait
    2. {
    3.     public TraitName traitName;
    4.     // chance to trigger skill
    5.     public float triggerChance;
    6.     public float triggerArmorPercent;
    7.     // effective range
    8.     public float range;
    9.     // for self only trait, such as Alone Wolf
    10.     public bool alliesNotAllowInRange;
    11. }
    12.  
    13.  
    Second, I have

    Code (CSharp):
    1.  public static List<Trait> traitList = new List<Trait>();
    2.  
    3.     public static void StartUp()
    4.     {
    5.         Trait t = new Trait();
    6.  
    7.         t.traitName = TraitName.Armor_Improve_I;
    8.         t.triggerChance = 100;
    9.         t.triggerArmorPercent = 0; // >=
    10.         t.range = 0;
    11.         t.alliesNotAllowInRange = false;
    12.  
    13.         traitList.Add(t);
    14.  
    15. // more traits below
    16.  
    17.  
    18.     }
    Then I want to
    Code (CSharp):
    1.  
    2.         foreach(TraitName tn in attacker.GetComponent<RobotAI>().data.traits)
    3.         {
    4.            // How to find the member in traitList that have the same name as tn?
    5. // So I can get other class member value?
    6.  
    7.         }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You use a nested loop, so
    Code (csharp):
    1.  
    2. foreach(TraitName tn in blah blah)
    3. {
    4.    foreach(trait tr in traitlist)
    5.    {
    6.       if(tn.name == tr.name)
    7.          {
    8.              //do something;
    9.           }
    10.     }
    11. }
    12.  
    I don't know if I got the syntax just right but you should be able to figure it out, assuming that's what you were asking.
     
    Last edited: Jan 12, 2019