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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Scripting' started by SomeVVhIteGuy, Apr 4, 2018.

  1. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class StatBase
    6. {
    7.  
    8.  
    9.  
    10.  
    11.  
    12.     public int statBase { get; set; }
    13.     public int statFinal { get; set; }
    14.     public string statName { get; set; }
    15.     public string statDescription { get; set; }
    16.  
    17.     public List<StatBonus> addedStats { get; set; }
    18.  
    19.  
    20.     public StatBase(int istatValue, string istatName, string istatDescription)
    21.     {
    22.         this.addedStats = new List<StatBonus>();
    23.         this.statBase = istatValue;
    24.         this.statName = istatName;
    25.         this.statDescription = istatDescription;
    26.    
    27.     }
    28.     public void AddBaseStat(int nstatBonus)
    29.     {
    30.         this.statBase = this.statBase + nstatBonus;
    31.  
    32.     }
    33.     public void AddStatBonus(StatBonus currentBonus)
    34.     {
    35.         this.addedStats.Add(currentBonus);
    36.  
    37.     }
    38.  
    39.     public void RemoveStatBonus(StatBonus currentBonus)
    40.     {
    41.         this.addedStats.Remove(currentBonus);
    42.     }
    43.  
    44.     public int ReturnFinalStatValue()
    45.     {
    46.         statFinal = statBase;
    47.         this.addedStats.ForEach(x => this.statFinal += x.BonusValue);
    48.  
    49.         return statFinal;
    50.     }
    51. }
    52.  
    and all that statBonus does

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StatBonus
    6. {
    7.  
    8.     public int BonusValue { get; set; }
    9.  
    10.     public StatBonus(int bonusValue)
    11.     {
    12.         this.BonusValue = bonusValue;
    13.  
    14.         Debug.Log("new stat bonus added");
    15.     }
    16. }
    17.  
    So yeah I don't know. I'm pretty sure im calling List<statBase>.removeStatBonus right. but it doesn't remove that statBonus.
     
    Last edited: Apr 4, 2018
  2. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    selfish bump I still cant figure it out
     
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    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.
     
    SomeVVhIteGuy likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I think you're misunderstanding how Lists and List.Remove works.

    Your "List<StatBonus> 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.
     
    SomeVVhIteGuy likes this.
  5. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    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?
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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?).
     
  7. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    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.