Adding class to list

hello,
I’m poorly using List and I’d like to have an advice on how to use it efficently

In one Script I’ve created a class Bonus() and a list of bonus

public class BonusMalus{
        public string name; //name of the status (stun, def+3, etc..
        public int remains; //nb of remaining turns
    }
publicList<Bonus> bonusList;

Now in another script (that got access to my main script with the bonus class in it)
I try to add something to the list with the help of Add(“name”,2)

actors.script.bonusList.Add("name",2);

but I get an error “error CS1501: No overload for method Add' takes 2’ arguments”

Then I tried to add directly an object of Bonus class like
actors.script.bonusList.Add(new actors.script.Bonus(“name”,2));

but there i cannot have access to the bonus class (that is public)
How should I correctly proceed ?

It looks like you’re trying to use Add() to initialize the class and add it at the same time which doesn’t work.

Initialize it first, set the variables you want, then add it to the list. You also may have some syntax errors in there… What environment are you using?

The method Add can only accept as parameter one object of type Bonus… you are sending a string and a integer.

You would need to define a new object of type bonus and then add that object to the list.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


[System.Serializable]
public class Bonus
{
    public string name;
    public int remains;
}

public class BonusManager : MonoBehaviour
{
    public List<Bonus> bonusList;

    void Awake()
    {
        Bonus b = new Bonus();
        b.name = "John";
        b.remains = 5;
  
        bonusList.Add(b);
    }
}

Even better, you can define your parameters in a constructor (will save you 2 lines of code in your context…)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


[System.Serializable]
public class Bonus
{
    public string name;
    public int remains;

    public Bonus(string _name, int _remains)
    {
        name = _name;
        remains = _remains;
    }
}

public class BonusManager : MonoBehaviour
{
    public List<Bonus> bonusList;

    void Awake()
    {
        Bonus b = new Bonus("John", 5);  
        bonusList.Add(b);
    }

}
5 Likes

This cannot work because those two parameters are not what Add() is expecting. Your list can only contain Bonus objects, but you’re trying to add something else.

So you need to create a Bonus (or is it BonusMalus?) object first. That is usually done through a constructor, which your class seems to be missing. Then, you add the constructed new object to your list.

Edit: Well, what sluice said.

1 Like

while I was typing sluice answered it perfectly.
you’re class BonusMalus and you List aren’t at all related. If you made public class Bonus { string name; int turns; } you could then make a list of bonuses List bonuses = new List(); but adding to that bonuses.add(*****) would require an already made bonus, or you’d need to define a constructor like sluice shows how to so you could call bonuses.add(new Bonus(“name”, 2));

a constructor will make it easy to make a public void like

public void AddNewBonus(string name, int turns) {
   bonuses.add(new Bonus(name, turns));
}

though kind of redundant in such a simple case.

2 Likes

Thanks,
So i’ve understood now that it was waiting a Bonus type (yup I made mistake when rewriting the name of the class in the forum, typo error)

Oddly while sluice method works well, It returns error when I tried to do the way malkere showed :
while

Bonus b = new Bonus();
b.name = name;
b.remains = turns;

bonusList.Add(b);

works ,

the variant of malkere doesn’t work returning an error:

bonusList.Add(new Bonus(name,turns));

So we cannot shorten the way a class is created?

2 Likes

@Eidern , did you look at my second SPOILER tag?

I show two ways:

  1. Without a constructor, the long way
  2. With a constructor

You’re doing a key and value. Look into Dictionaries.

Instead of a constructor, you could use a C# object initalizer. They have a bit of a wonky syntax, but it would be this:

Bonus b = new Bonus { name = name, remains = turns };

You should use a constructor instead if the object doesn’t have a valid state without values for the variables. That’s probably what you’re dealing with, as a bonus with no name isn’t very interesting.

yes, I’ve seen it, it just bugs me a little that we must init a var first (Bonus b =new Bonus(“John”, 5); ) instead of Adding directly to a list with the “new” operator…
Well I just have to get used with it and it’ll be allright :wink:
thanks again

In fact you can add it directly in one line of code after following sluice’s second suggestion. However, I wouldn’t recommend it.

You can think of programming a little like solving algebra. With practice, you can start skipping steps, like flipping signs and such. However, if you try to get too clever and skip many steps, you might a) make an error and b) have trouble spotting it later. It also becomes difficult for another person, especially if they are not as experienced, to read your solutions. Try to do just one thing with each line of code.

3 Likes