How bad(expensive) would it be to use a self destruct timer like this?

Before seeing Rick’s solution, I created this self destruct script. I actually laughed when I saw how simple and more straight forward Rick’s solution was. Is my script too expensive since it relies on Update()?
Thanks!

using UnityEngine;

public class DestroyTimer : MonoBehaviour
{
    [SerializeField] float timeUntilDestroyed = 2f;
    float currentTime = 0f;
    private void Update()
    {
        currentTime += Time.deltaTime;
        if (timeUntilDestroyed <= currentTime)
        {
            Destroy(gameObject);
        }
    }
}

Hi,

Good job on challenging yourself! :slight_smile:

What do you mean by “too expensive”? Of course, your solution is more expensive than using the second parameter of the Destroy method but that does not automatically make it “too expensive”. On our modern computers, we probably won’t notice any difference unless we had thousands of DestroyTimer objects in our game and would replace it with Rick’s solution.

Thanks so much for your explanation

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms