(Solved?)my remove function calling .Remove is not working

Going to have to past a good bit of code here.

I have a stat system setup, statBase both sets up a stat, and has functions to modify that stat in game including removeStatBonus. StatBonus is a separate script which basically just returns an int to be added. characterStats contains lists of statsBase’, which are then applied statBonus’ as they are… well added.

ill post how I’m calling it first. In characterStats

public void updateEffectedStats(int statRefNumber)
{
if (statRefNumber == 2) //CON
{

stats[6].RemoveStatBonus(new StatBonus(conSlowReference));
conSlowReference = stats[2].ReturnFinalStatValue();
stats[6].AddStatBonus(new StatBonus(conSlowReference));

}
//this works correctly off of start, adding 5 to the health stat. but when i call this function later,
//it does not remove the last statBonus as it should. it just updates the slowReference and adds a new statBonus
}[/code]

To show exactly what statBase does heres the full script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StatBase
{





    public int statBase { get; set; }
    public int statFinal { get; set; }
    public string statName { get; set; }
    public string statDescription { get; set; }

    public List<StatBonus> addedStats { get; set; }


    public StatBase(int istatValue, string istatName, string istatDescription)
    {
        this.addedStats = new List<StatBonus>();
        this.statBase = istatValue;
        this.statName = istatName;
        this.statDescription = istatDescription;
    
    }
    public void AddBaseStat(int nstatBonus)
    {
        this.statBase = this.statBase + nstatBonus;

    }
    public void AddStatBonus(StatBonus currentBonus)
    {
        this.addedStats.Add(currentBonus);

    }

    public void RemoveStatBonus(StatBonus currentBonus)
    {
        this.addedStats.Remove(currentBonus);
    }

    public int ReturnFinalStatValue()
    {
        statFinal = statBase;
        this.addedStats.ForEach(x => this.statFinal += x.BonusValue);

        return statFinal;
    }
}

and all that statBonus does

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

public class StatBonus
{

    public int BonusValue { get; set; }

    public StatBonus(int bonusValue)
    {
        this.BonusValue = bonusValue;

        Debug.Log("new stat bonus added");
    }
}

So yeah I don’t know. I’m pretty sure im calling List.removeStatBonus right. but it doesn’t remove that statBonus.

selfish bump I still cant figure it out

stats[6].RemoveStatBonus(new StatBonus(conSlowReference));

This won’t work. I think the difference between value and reference types is tripping you up here.

What you are doing is creating a new StatBonus object, then telling the list to remove it from the list. Since it’s a brand you object you just created, it won’t exist in the list, right? So nothing will happen if you try to tell the list to find and remove an object that matches what you tell it to look for.

1 Like

I think you’re misunderstanding how Lists and List.Remove works.

Your “List addedStats” is a list of StatBonus references. You’re attempting to remove a StatBonus from the list as if StatBonus was a value type. If your List was of a value type, like int, your remove would work.

Instead you need to search the list for an element that has a BonusValue that matches the bonus you want to remove, and then remove that reference from the list. Or instead of passing in a reference to a new StatBonus object, you pass in the specific reference to the object already in the list that you want to remove.

1 Like

okay so I read this and thought about it. and theres really no need for me to be using statBonus at all right? all it does is store as an int anyway. But other things cant see it as an int because it… well its just not a simple int.

SO I just went back and made everything an int instead of a statBonus and the system is now working great.

I understand this won’t be the easiest question to answer but given when I showed in the OP can you think of any reason to actually use statBonus?

I agree that StatBonus just being a class that stores a single public int makes StatBonus as written unnecessary. If you had 2+ different variables you wanted to track together, or some methods to the class, then you’d want to use it. For example, if I was doing your project I’d probably have StatBonus also have a string description of the bonus, or perhaps have it include what stat for your character it is actually affecting (do you only have a single stat to modify for your character?).

well no I currently have 14 actually. I’m using it to store hard stats(Str,Dex,Con,Wis,Int,Cha) along with health and other stats that change.

right now the stats themselves have 2 descriptors that can be called(two so I can write it as “your” and “My”). But thats not to "catalog"them.
characterStats has a List of StatBase’s (which I now realize is kinda misnamed) named stats. whenever I add a new stat bonus it does that within the list reference already. I call it like stats[6].AddStatBonus(2 * conSlowReference);
SO my added stats get stored separately anyway, and theres no need to catalog them with descriptors right?

The only thing I could think of that would need to reference a descriptor of the statBonuses would be like if I wanted to hover a stat, and see where its coming from.

like 10 “base”, 4 “from perks”, 13 “from gear”.

which would be cool to implement at some point. but if its going to cause problems like the one I was having I don’t think its worth the headache right now.