I am working on a racing game, which has speedboosters on the ground. When a vehicle touches it, a script attached to the speedbooster instantiates a Fire prefab and then destroys it after 2 seconds.
This is my script (in C#), which used to work fine but now the Destroy() function doesn't have any effect and I can see the instances of my Fire prefab in the game hierarchy:
private GameObject _fireModel, _fireObj;
void Start ()
{
_fireModel = Resources.Load("Fire1") as GameObject;
_watch = new Stopwatch();
enabled = false; // (don't call Update() on each frame)
}
void OnTriggerEnter (Collider col)
{
_fireObj = Instantiate(_fireModel) as GameObject;
enabled = true; // (do call Update() on each frame)
_watch.Start();
}
void Update ()
{
_fireObj.transform.position = _player.position;
if (_watch.ElapsedMilliseconds >= _fireDurationMs)
{
Destroy(_fireObj);
_watch.Stop();
_watch.Reset();
enabled = false; // if I remove this line,
// I get a console error saying _fireObj
// has been destroyed! Yet I can still
// see it on the game and in the hierarchy
}
}
Do you see a case where this script wouldn't destroy the instance of Fire1?
If two OnTriggerEnter events occur within _fireDurationMs, you will get two new instantiations of the objects and only 1 will get destroyed, because you keep a reference only to the latest fire.
If this was my game, I would improve the design a bit. Instead of making the speedboosters responsible for destroying the fire, you could make a script called something like "DestroyAfterMilliseconds", which destroys the gameObject it is attached to after the preset amount of time occurs, and attach it to the fire prefab. Then each instance of the fire would be responsible for destroying itself and the speedboosters would have less responsibilities. That same script could then be re-used on any other objects that you want to give limited lifetime to. Splitting up the responsibilities would also make the code easier to debug and maintain.