0

I rewrote this in various ways, but nothing up to now would do the trick. I made sure that everything is tagged correctly and has the right scripts attached as you can see in the pic.

This is the stripped code of the class (CrowdControl) that is supposed to:

  • find all objects tagged "Fighter"

  • fill a list with all FighterControl scripts attached to them

    private List<FighterControl> fightersScripts;
   

    private void Start()
    {
        InitializeFighters();
    }

    private void InitializeFighters() // first line that the NullReferenceException is pointing at !!!!
    {
        GameObject[] tempfightersGM = GameObject.FindGameObjectsWithTag("Fighter");
        for (int i = 0; i < tempfightersGM.Length; i++)
        {
            fightersScripts.Add( tempfightersGM[i].GetComponent<FighterControl>()); // second line that the NullReferenceException is pointing at !!!!
        }
    }

Pic of the editor including the inspector view of the 2 game objects

  • Is `GameObject` null? – DavidG Feb 27 '21 at 12:34
  • @DavidG `GameObject` is a type ... `GameObject.FindGameObjectsWithTag` is a `static` method ;) – derHugo Feb 27 '21 at 17:45
  • @Abenfante your list `fightersScripts` is not initialized! You should already declare it as `private readonly List fightersScripts = new List();` – derHugo Feb 27 '21 at 17:47
  • Btw alternatively you could probably simply use `fighterScripts = FindObjectsOfType().ToList();` (or use an array and skip the `ToList`) – derHugo Feb 27 '21 at 17:49

0 Answers0