Can't make Destroy() work for me.

I have:
SomePrefab someRef = Instantiate(SomePrefab, blah,blah…);

then a little later:
Destroy(someRef);

Nothing happens. The object held in someRef is still on my screen. Tried it with different types of objects. What I actually want to use it on is a canvas prefab that acts as a popup menu, but like I say it’s not even working for me on normal instantiated objects.

What have I missed?

Give “Destroy(someRef.gameObject);” a try and see if that works for you. If not, please include a code example so we can look a little deeper into the issue.

1 Like

Destroy destroys the component you’re pointing at. So if it’s a gameobject, it destroys the whole gameobject, if it’s a component (script, collider), it destroys that one.

This should still work if the instantiation and destruction happens on the same frame, but I haven’t tried that, so there migth be some caveats.

If you’re doing this in editor time (editor script not running in play mode), you’ll have to use DestroyImmediate

2 Likes

Destroy(someRef.gameObject); worked, thank you very much krougeau, and thanks for the extra explanations Baste.

1 Like