1
\$\begingroup\$

What is the difference between using Object.Destroy() and GameObject.Destroy() in Unity?

I found the following info about that: here and here, but it did not answer my question.

So, could someone explain the matter foolproof, please?

\$\endgroup\$
2
  • 2
    \$\begingroup\$ AFAIK, they are the same. If you look at the documentation page of GameObject, the Destroy function is inherited from parent class (which is Object) \$\endgroup\$
    – Hellium
    Dec 22, 2017 at 10:04
  • 1
    \$\begingroup\$ @Hellium it's a simple answer, but still worth posting as an answer that can be marked Accepted. \$\endgroup\$
    – DMGregory
    Dec 22, 2017 at 14:04

1 Answer 1

1
\$\begingroup\$

None. GameObject.Destroy() is inherited from Object.Destroy(). Both statements refer to the same method.

This is not explicitly clear in the official API documentation, but most programmers can assume that to be the case based on the fact that both GameObject and Object contain the method, and GameObject inherits from Object. It can then be confirmed through IDEs or through the unofficial decompied source-code by MattRix on GitHub (GameObject's source code does not contain the Destroy() method, meaning it can only come from inheritance).

As such, you will also find the Destroy() method on anything else in the inheritance tree of Object. For example, Component also inherits from Object, and so you can add a Destroy() statement within any component's code, without explicitly including Object. before it.

Note: My answer is independent, but some credits to Hellium for answering correctly first in the OP.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .