Destroy(gameObject, 1f); vs Destroy(sparkles, 1f);

Heya, in this chapter, on the last challenge, Rick used “Destroy(sparkles, 1f);” and i myself used “Destroy(gameObject, 1f);” both is working, but since Rick have mentioned it a couple of times now, that u could use “gameObject” when its referring to itself inside its own method, wouldn’t it be better to use the gameObject instead of sparkles, since it then should be 100% clear that im calling it on itself inside its own method ?
First time asking in here, so i don’t know if im explaining myself properly…
Basicly i just want to know, which code would be best to use. :slight_smile:
Regards Henrik

Hi,

Welcome to our forum! :slight_smile:

Which approach is the best depends on what you want to achieve. sparkles contains a reference to a component, not to a game object. If you want to destroy a specific component, call Destroy(sparkles). If you want to destroy a game object including its components, call, for example, Destroy(gameObject).

Your current script instance is not the game object. You can access gameObject because your script inherits from MonoBehaviour. Unity automatically assigns the reference to the game object your current instance is attached to the gameObject variable.

Is this what you wanted to know?

Yes! thank you, thats a great explanation, it will help me to not make that assumption again :slight_smile:
Thank you very much.

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