Changing variable of instanced

Hey,

I have a script the instantiates “butterflies”.
Each instance has a script on it for controlling flying behaviour.
In that script, there is a boolean to control whether the butterfly can land or not. (isLandingButterfly)
So far everything works fine.

I now want to change the boolean “isLandingButterfly” for some of the butterflys as they are instatiated. But for some reason I’m getting a Null Reference Exception at line 21

NullReferenceException: Object reference not set to an instance of an object
ButterflyGenerator.Start () (at Assets/ButterflyGenerator.cs:21)

I know I’m doing something dumb, does anyone know what it is?

public class ButterflyGenerator : MonoBehaviour
{
    public GameObject butterfly;
    public int butterflyNumber;
    public List<GameObject> butterflyList = new List<GameObject>();
    public List<ButterFlyControler> butterflyControlerList = new List<ButterFlyControler>();
    public int maximumNumberOfLandingButterflys = 5;

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < butterflyNumber; i++)
        {
            butterflyList.Add( Instantiate(butterfly));
            if (i < maximumNumberOfLandingButterflys)
            {
                butterflyList[i].GetComponent<ButterFlyControler>().isLandingButterfly=true;

                //butterflyControlerList.Add (butterflyList[i].GetComponent<ButterFlyControler>());
                //butterflyControlerList[i].isLandingButterfly = true;
            }
        }
    }

}

Thanks in advance

i would do something like this…

void Start()
    {
        for (int i = 0; i < butterflyNumber; i++)
        {
            var go = Instantiate(butterfly);
            if (i < maximumNumberOfLandingButterflys)
            {
                   go.GetComponent<ButterFlyControler>().isLandingButterfly=true;
            }
            butterflyList.Add( go);
        }
    }

There are a bunch of ways that your script may be getting into a bad state. Because your lists are public, they might be in a non-empty state when this runs. If they’re populated with outdated or null references, this would be the error I’d expect.

A few things you can do. Any of these would probably fix it, but all of them are good practice.

  1. Make butterflyControlerList private instead of public.
  2. add butterflyContolerList = new List(); to the beginning of Start()
  3. use a temp variable to store the instantiated object, so it’s easier to know for sure that you’re working with the right object.
for (int i = 0; i < butterflyNumber; i++)
        {
            GameObject go = Instantiate(butterfly);
            butterflyList.Add(go);
            if (i < maximumNumberOfLandingButterflys)
            {
                go.GetComponent<ButterFlyControler>().isLandingButterfly=true;

                butterflyControlerList.Add (go.GetComponent<ButterFlyControler>());
// the last line was redundant with line 7 above
            }
        }