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