Should I destroy GameObject manually when it goes out of scope? (Since GameObject class must be a wrapper around native class, the answer - yes. But i’m not sure.)
In this code GameObject is locally scoped.
void foo()
{
GameObject mGameObject = new GameObject("name");
// Destroy(mGameObject); => WE WON'T CALL THIS METHOD
}
We didn’t call Destroy(mGameObject); => it will persist in memory (memory leak?) Should we always manually destroy locally scoped GameObjects (and similar objects then have Destroy() method) when they go out of scope?
No, you don’t need to do this. And in this example (I miss remember the exact message) but it would tell you that you’re trying to access something out of scope as an error.
EDIT: Ok, so for one thing, I doubt you can even “new” a game object. Shouldn’t all game objects be instantiated? And if so, a game object will remain instantiated (and use up resources) until you manually destroy it. Here is an older question on a similar subject Do un-active game objects still use memory? - Unity Answers
Now, if we’re talking other classes that you allocate memory for with new, as far as I’ve checked, there is no “delete” to accompany the new like in c++, so I assume freeing up memory is done automatically. At least I sure as hell hope so.