0

I was under the impression this would populate my list with instances (or references to) of my second class but when run I just get a list filled with none

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

public class DataStructure : MonoBehaviour
{
    public int size;
    public List<DataEntry> dataList = new List<DataEntry>();

    void Start()
    {
        for (int x = 0; x < size; x++)
        {
            DataEntry newEntry = new DataEntry();
            dataList.Add(newEntry);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataEntry : MonoBehaviour
{
    public string text;
    public int number;
}
davidsbro
  • 2,761
  • 4
  • 23
  • 33
LayLae
  • 1
  • 1
  • Don't use `new` to instantiate `MonoBehaviour`s. Easiest solution in this case without further information is to make `DataEntry` no longer a `MonoBehaviour`: `public class DataEntry { ... }`. If you do need it to be a `MonoBehaviour` for the purposes of attaching to GameObjects, you should instead use `AddComponent`. – Ruzihm Nov 19 '21 at 18:19
  • what is the value of size? none – Leandro Bardelli Nov 19 '21 at 18:34
  • @LeandroBardelli Not necessarily. `size` could have a nonzero value assigned in the unity inspector. – Ruzihm Nov 19 '21 at 18:43

0 Answers0