- Home /
Objects not being added to a list
So right now I'm trying to set up a turn based combat system, and I need to make a list for turn order. I have a list set up which is supposed to store a class called AgentInit. However, nothing is being stored in the list.
var initiative = new List.<AgentInit>();
for (var g : int = 0; g < enemyList.length; g++) {
initiative.Add (new AgentInit (enemyList[g].GetComponent(AgentStats).speed, enemyList[g]));
print(enemyList[g].GetComponent(AgentStats).agentName);
}
for (var h : int = 0; h < playerList.length; h++) {
initiative.Add (new AgentInit (playerList[h].GetComponent(AgentStats).speed, playerList[h]));
print(enemyList[h].GetComponent(AgentStats).agentName);
}
initiative.Sort();
That is the section of code which should be adding the objects to the list. Those lines that say to print work perfectly fine, and I've looked all over and have no clue what the problem is. (BTW the two variables stored in AgentInit are an int and a GameObject)
I'm not super experienced with code, any help is appreciated.
I don't know where you check the content of the list, but it's definitely not empty. Only thing I found was that your second print also looks into the enemy list, is that's confusing you.
It definitely is empty. The inspector says it's empty and I'm getting an error when I try to access element 0. I did fix that print statement though. Here's a picture if it helps. http://imgur.com/a/ZVekm
I still have no idea where you're reading the 0 index. The error says line 81. Where is that? Am I even looking at the same script? Please post the relevant parts in total. The screenshot doesn't really say anything.
Answer by Mraction2012 · Jun 18, 2017 at 09:34 PM
that line "var initiative = new List.();" should be called once, e.g in start()
Answer by dreg_master · May 17, 2018 at 02:04 PM
For me, I needed to add my Human Class to MonoBehaviour otherwise when i instantiated a human and tried to add it, it wouldn't work. [code] using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PopulationManager : MonoBehaviour { [SerializeField] private Image PopulationIcon; [SerializeField] private Text PopulationText; [SerializeField] private List Humans; //public string manualString;
// Use this for initialization
private void Start()
{
PopulationIcon = GetComponentInChildren<Image>();
PopulationText = GetComponentInChildren<Text>();
Humans.Add(new Human());
Humans.Add(new Human());
Humans.Add(new Human());
foreach (Human h in Humans)
{
Debug.Log(h.ToString());
}
}
// Update is called once per frame
private void Update()
{
int i = Humans.Count;
PopulationText.text = "" + i;
}
} [/code]
[code] internal class Human : $$anonymous$$onoBehaviour { public GameObject HumanPrefab; private static int HEALTH_START; private static int HEALTH_A = 100; private static int HEALTH_B = 70; private static int HEALTH_C = 50; private static int HEALTH_D = 25; private static int HEALTH_E = 0;
private int health;
public Human()
{
health = HEALTH_START;
}
public override string ToString()
{
return base.ToString();
}
} [/code]
Your answer