Destroy(this); discussion

When I created the Block.cs script with Unity 2020.3.13.f1, the auto-generated code was the following.

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Destroy(this);
    }

Out of curiosity, I tried it and as I expected it did not work. According to the documentation

Destroy(this); removes this script instance from the game object

So I was wondering what will be a valid case to do this? I can only think of freeing some memory if the script allocates a lot of memory for whatever reason but I am not entirely convinced that this is the most common scenario.

Thank you.

Hi Angelos,

Destroy(this); destroys the current script instance. To be honest, I cannot come up with any example where it could make sense to destroy a component apart from “freeing some memory”. :confused:

Please feel free to ask our helpful community of students over on our Discord chat server.

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

Privacy & Terms