I’ve made a script called AmmoStore to act as a free list for bullets. It has an ArrayList of bullets that I create in Awake. Its value is not modified elsewhere in the code, but for some reason, when I access it, it is null unless I assign it again.
public ArrayList bullets;
void Awake ()
{
Debug.Log("AmmoStore Awake");
bullets = new ArrayList();
Debug.Log("bullets = " + bullets);
}
public Bullet getBullet()
{
Debug.Log("bullets = " + bullets);
if(bullets == null)
{
bullets = new ArrayList(); //This line keeps getting run and I don't know why
}
if(bullets.Count > 0)
{
Bullet retBullet = bullets[0] as Bullet;
bullets.Remove(retBullet);
return retBullet;
}
return makeBullet();
}
Line 16 is always run, even though I’ve just assigned bullets in Awake.
The output is always
AmmoStore Awake
bullets = System.Collection.ArrayList
bullets =
I’m a bit new to Unity. Is there something about the scope of Awake that I’m missing?