Question by
aferlane · Feb 06, 2016 at 11:55 PM ·
generic list
Help with populating a list
I'm trying to populate a generic list with a bunch of unique NPC's with the following code. The debug output shows me that during each step of the first for loop, a new, unique NPC is being generated, but each step of the way, the Debug.Log outputs the same name all three times. When I go through the full list at the end, every single NPC in the list is exactly the same. Any idea what I'm doing wrong here?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NPCTracker : MonoBehaviour
{
public NPC npc;
public List<NPC> npcList = new List<NPC>();
void Start()
{
for (int i = 0; i < 50 ; i++)
{
npcList.Add(npc.CreateNewNPC());
Debug.Log(npc.GetFullName());
Debug.Log(npcList[i].GetFullName());
Debug.Log(npcList[0].GetFullName());
}
for (int i = 0; i < npcList.Count; i++)
{
Debug.Log(npcList[i].GetFullName());
}
}
}
Comment
I changed the NPC class to inherit from Scriptable Object ins$$anonymous$$d of $$anonymous$$onoBehaviour, and that seems to have worked, though I'm having trouble wrapping my head around it. Is anyone able to explain why it didn't work when it was using $$anonymous$$onoBehaviour?
Your answer
![](https://koobas.hobune.stream/wayback/20220612063255im_/https://answers.unity.com/themes/thub/images/avi.jpg)