0

I am trying to make an editor extension for Unity that reads a class named Attack, but C# is not throwing back a NullReferenceException when try to set the object.

This is where the problem lies:

void OnGUI(){
    EditorGUIUtility.labelWidth = 60;
    GUILayout.Label("Attack Manager", EditorStyles.boldLabel);

    Attacks[DataStrings.Length - 1] = new Attack();
    if (DataStrings != null){
        foreach(string Dataline in DataStrings){
            Attacks[i] = new Attack();
            Attacks[i] = JsonUtility.FromJson<Attack>(DataStrings[i]);
            NewAttack(true);
            i++;
        }
    }
ThatDGuy
  • 11
  • 3

1 Answers1

0
Attacks = new Attack[0];

Will initiate your Array. HOWEVER I suggest you replace the array with a list

Initialize it like this

List<Attack> Attacks = new List<Attack>()

Then inside your method

    if (DataStrings != null)
{
    foreach(string Dataline in DataStrings)
    {
    var attack = new Attack();
        attack  = JsonUtility.FromJson<Attack>(DataStrings[i]);
        Attacks.Add(attack)
    }
}
Dyna Dave
  • 149
  • 1
  • 5