Array of Animator doesn't work, but individual does

I made the script work, but not the way I wanted, so I’ll just pop a quick question because I’ve no idea Why it doesn’t work the original way. I tried doing this…

public GameObject[] obj; //I have 2 objects attached to this in the Inspector
private Animator[] ani;

public Awake(){
    for(int i = 0; i < obj.Length; i++){
        ani *= obj.GetComponent<Animator>(); //This is where the error puts me After I run the game*

}
} //And no worries, both game objects had an animator component
This gives me an error as I didn’t refer to any of the animators. exact error is:
NullRefereneException: Object reference not set to an instance of an object
But when I try to put the animators in seperate variables, while keeping the game objects in an array, it works. ie…
public GameObject[] obj;
private Animator ani1;
private Animator ani2;

public Awake(){
ani1 = obj[0].GetComponent();
ani2 = obj[1].GetComponent();
}

You are trying to build an Animator Array as if it was a List. The Unity Array number needs to be set at creation so you cant iterate through the for loop like that and add to your array. You are also using obj.getcom… instead of obj*.get…*
Instead, use Lists
using System.Collections.Generic;

//class

private List ani;

public void Awake(){
ani = new List();
for(int i = 0 ; i < obj.Length; i++){
ani.Add(obj*.GetComponent();*
}
}
WUCC check for errors.

There is no thing like an Animator Array, But yes, you make custom lists and make them perform the same as the array.
Refer to the example below for storing to list and retrieving from it.

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

private List<Animator> animlist;
public GameObject[] Elements;

void Start () {
		
		animlist = new List<Animator>();
		for(int i=0; i<Elements.Length; i++)
		{
				animlist.Add(Elements*.GetComponent<Animator>());*
  •  }*
    
  • }*

  • void readList(){*

  •  	foreach(Animator anim in animlist)*
    
  •  	{*
    
  •  			anim.SetBool ("trigger", true);*
    
  •  	}*
    
  • }*