Search Unity

Question how to access lists dynamically?

Discussion in 'Scripting' started by totallylegitemail669, Jan 7, 2024.

  1. totallylegitemail669

    totallylegitemail669

    Joined:
    Sep 27, 2018
    Posts:
    2
    Hey friendos, newbie question here;
    I'm currently using a list to store data for each weapon the player can use, but iI don't know how to reference it efficiently. my current setup is;
    Code (CSharp):
    1. [System.Serializable]
    2. public class Weaponz{    //creates list with data of every weapon
    3.     public string name;
    4.     public float placeholderStat1, placeholderStat2;
    5.    
    6. }
    7. [ExecuteInEditMode]
    8. public class Test : MonoBehaviour
    9. {
    10.     public List<Weaponz> weapons_Info;
    11.    
    12.     public Weaponz pistol = new Weaponz(), shotgun = new Weaponz();
    13.     public float activeweapon;
    14.    
    15.     public float currentPlaceholderStat1, currentPlaceholderStat2; //active memory of weapon
    16.    
    17.     void Start()            //change weapon stats
    18.     {              
    19.         if(activeweapon == 1){
    20.            
    21.             currentPlaceholderStat1 = pistol.placeholderStat1;
    22.             currentPlaceholderStat2 = pistol.placeholderStat2;
    23.         }
    24.        
    25.         if(activeweapon == 2){          
    26.             currentPlaceholderStat1 = shotgun.placeholderStat1;
    27.             currentPlaceholderStat2 = pistol.placeholderStat2;
    28.         }
    29.     }
    30. }
    31.  
    basically I want to be able to pull variables in one function, if that makes sense?
    thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,708
    Olipool and spiney199 like this.
  3. totallylegitemail669

    totallylegitemail669

    Joined:
    Sep 27, 2018
    Posts:
    2
    i did that, works great! thanks dude!