Day 11: Instantiate&Destroy

We talked about the spawn manager to add objects to the scene that are not initially present. Today let’s look specifically at how these objects can be Instantiated in a scene and how they can be removed.

Instantiate(Object obj)

this method allows us to add a game object in our scene, we can pass also only the object that we want to insert, but the necessary elements for a Game object are three.
The object we want to instantiate, its position determined by a Vector3 and its rotation which we manage with a quaternion.

another override of this method allows us to instantiate our game object already a child of another game object

three of the ten variants of overrides of this function

A game object that is instantiated can also be assigned to a variable so that it can be called within the code.

Destroy(Object obj)

this method allows us to permanently remove an object. It has two overrides one that only requires the game object to be destroyed and a version with a float that allows us to create a delay before removal.

it is very useful when we want to remove from the scene some game object, in my example, the bullet destroys itself and the game object enemy that is going to touch with the method OnTriggerEnter2D.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store