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. Dismiss Notice

How can I create a list where each item stores different variables?

Discussion in 'Scripting' started by Winter_XYZ, Dec 30, 2020.

  1. Winter_XYZ

    Winter_XYZ

    Joined:
    Aug 19, 2015
    Posts:
    13
    Title explains what I'm trying to do. I know how to make lists, but I can't seem to figure out how to make it so each item stores a range of variables.

    The best example would be a list that stores enemies, and each enemy has something like:

    Code (CSharp):
    1. float health = 20.5f;
    2. int attack = 25;
    3. bool isCursed = false;
    Then I can just add more "enemies" to the list, but each enemy always has these 3 variables.

    Could somebody help me out in letting me know how to do this?
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    This is basic programming, make it a class then make a list of that class.

    Code (CSharp):
    1. public class EnemyInfo
    2. {
    3.     public float health = 20.5f;
    4.     public int attack = 25;
    5.     public bool isCursed = false;
    6. }
     
  3. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    The word you're looking for is "class".
    Code (csharp):
    1. [Serializable] public class Enemy
    2. {
    3.    public float health = 20.5f;
    4.    public int attack = 25;
    5.    public bool isCursed = false;
    6. }
    7.  
    8. Enemy e1 = new Enemy() { health = 10f, attack = 30, isCursed = true };
    9. //you can modify it
    10. e1.isCursed = true;
    11. //you can make a list of them
    12. List<Enemy> enemies = new List<Enemy>();
    13. enemies.Add(e1);
    14. enemies.Add(new Enemy() { health = 12f, attack = 25, isCursed = true });
    15.  
    16. // add a bunch of little guys in a loop
    17. for (int i = 0; i < 10; i++) {
    18. enemies.Add(new Enemy() { health = 1f, attack = 5, isCursed = true });
    19. }
    I added [Serializable] to the beginning of the class definition so that your enemies can be added and edited in the inspector.
     
    seejayjames likes this.
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Adding to that, the class can have methods (functions) as well:

    void die() {
    Destroy(gameObject); // maybe with animation, sound effect, etc.
    }

    so you'd call
    enemy1.die();
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    This will work if the class inherits from MonoBehaviour. If not then you cannot do that, plus this logic would lead to errors with the list having null references. You should explicitly remove items from the list and then Destroy/delete/dereference them.
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Ah, right, Destroy() is a wrong example of a method for this case. I was referring to methods that aren't required to inherit from Monobehaviour, like Debug.Log, setting variables, etc.
     
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    In the end though your info is not wrong. All classes and even structs can have functions/methods in them to help make things easier. Its only that we have to watch how we make things easier and try to not introduce too many bugs that cause problems down the road.
     
    seejayjames likes this.