To destroy or not to destroy? (494318)

Hello develoeprs,

Sometimes I have “helper” script, which should make something at the start (set default settings or something) and after it’s not needed.

For example, this one change sprite at the start.

using UnityEngine;

public class SetRandomSprite : MonoBehaviour
{
	[SerializeField] string spriteName = "blood";
	[SerializeField] byte spriteRangeFrom = 1;
	[SerializeField] byte spriteRangeTo = 9;

	void Start()
	{
		GetComponent<tk2dBaseSprite>().SwitchCollectionAndSprite(_sprite.collection, _sprite.GetSpriteIdByName(
			bloodName + Random.Range(bloodRangeFrom, bloodRangeTo + 1).ToString()));

		Destroy(this); // Should I?
	}
}

Question: is there any reason not to destroy such a scripts? Maybe it’s bad for GC if I have a lot of them?

Thank you!

Technically such a script wouldn’t do much harm i think…The only thing is that the variables that it uses occupy memory…If you will never ever need it again,probably it is best to destroy it to free up memory (Still,in your case i see it’s only a few bytes…However,if you would have arrays that would occupy a few megabytes,then you would really have to destroy them).

To destroy, or not to destroy is really a game level question. If you will never need this object again, Destroy it.

Ask yourself a better question though. Why do I need this object to do 1 thing? Can this 1 thing be applied to a different script?

It looks like you are simply running a method in this. Which is probably the result of an event. why cant you put this into the event.

Reasons you should:
Makes things cleaner
You dont have to create extra scripts to handle them.

Reasons you shouldn’t:
You can use these objects by dragging them into the inspector
Objects can simply do something, then delete themselves to make things cleaner.

Lets consider BloodSplatters. You shoot a guy, he squirts blood everywhere. There are puddles on the ground. Really gorey scene.

In this case, you need something to create the blood squirting, the squirts hitting the ground and things like this. This object will be visible for a number of frames. Thus, needs to be an object, and will need to eventually destroy it’s self.

This is about the same theory as what you have, but the objects are supposed to stay around.

The long and the short of it all is:

If you don’t need an object anymore, delete it. It always helps to keep things clean.

Thank you for the answers, guys!

So, as I understand, in most cases it’s better to destroy, but… In my case, when I instantiate GameObject with this script almost each frame, should I destroy it? Maybe it’s better to
GetComponent<tk2dBaseSprite>().SwitchCollectionAndSprite(_sprite.collection, _sprite.GetSpriteIdByName(bloodName + Random.Range(bloodRangeFrom, bloodRangeTo + 1).ToString())); from that script, which Instantiate this GameObject? But then it’s not so clean and I will not be able to use that script for other purposes…
What do you think?

Think of it this way. Unity handles about 65000 objects in the scene. After the 65000’th frame, your game will crash if you do not Destroy these objects.

My suggestion is to find an alternative method to do this, and never create the objects to start with.