I’m not very good at coding so I’m sure this is just something dumb that I’m doing wrong or not doing, but basically I have a list its holding a class in it and it seems to be working the first time I use it. Then if I add to that list again it overwrites the data in the first element with the newest one. I’m not exactly sure what I need to do to fix it so if someone could take a look and let me know whats wrong with it that would be a big help.
I believe you can ignore everything in the NewUnitStats () Method I was using that for testing and for calling AddPlayerToList () method but, I might be wrong.
public class MakeNewUnit : MonoBehaviour {
private CreateNewPlayerCharacter createNewPlayerCharacter;
public List<CreateNewPlayerCharacter> listOfPlayerCharacters;
public string unitName;
public string unitClassName;
public string unitRaceName;
public ScriptableObjectTest[] scriptObjTester;
void Start()
{
createNewPlayerCharacter = GetComponent<CreateNewPlayerCharacter>();
}
public void NewUnitStats()
{
unitName = createNewPlayerCharacter.baseUnitStats.unitName;
unitClassName = createNewPlayerCharacter.pickedClass.className;
unitRaceName =createNewPlayerCharacter.pickedRace.raceName;
Debug.Log("click working " + unitRaceName);
scriptObjTester = createNewPlayerCharacter.baseUnitStats.scriptObjSaveTest;
foreach (ScriptableObjectTest scriptObjTester in scriptObjTester)
{
int counterValue = scriptObjTester.value;
counterValue += 25;
//counterValue = counterValue + 20;
//Debug.Log(scriptObjTester.name);
//Debug.Log(scriptObjTester.value);
//Debug.Log(counterValue);
}
AddPlayerToList();
}
private void AddPlayerToList()
{
listOfPlayerCharacters.Add(createNewPlayerCharacter);
int index = listOfPlayerCharacters.IndexOf(createNewPlayerCharacter); // finds the number in the list
foreach (CreateNewPlayerCharacter unit in listOfPlayerCharacters)
{
Debug.Log("Unit " + index + " " + unitClassName + " " + unitRaceName);
}
}
}