Seeven
Seeven

Reputation: 1079

Destroy() not working anymore in Unity3D

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?

Upvotes: 1

Views: 6032

Answers (3)

Web Developer India
Web Developer India

Reputation: 677

This worked for me.

 Destroy(RigidbodyVariable);//First Line for Rigidbody
 Destroy(GameObjectVariable);//Second Line GameObject

Upvotes: 0

Burak Karasoy
Burak Karasoy

Reputation: 1690

You just need to add a script for your fire object.

using UnityEngine;
using System.Collections;

public class fireScript: MonoBehaviour {


    void Start () {
        Destroy(gameObject, 2f);
    }


    void Update () {

    }
}
`  

Now you don't have to control your fire object in the other script. Just initialize it and Firescript will handle the destroy method.

Upvotes: 0

LVBen
LVBen

Reputation: 2061

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.

Upvotes: 5

Related Questions