Updated: Nov 17, 2020
Unity Instantiate: Perfect for prefabs.
Unity has many awesome features you can use in C#. One of the most useful things to know in Unity is how to Instantiate. Since your reading this article, I’m going to assume you already have at least a very basic knowledge of Unity. If you don’t, I have other articles about Unity you can learn from on my site.
Unity Instantiate is usually used to add projectiles, AI Enemies, particle explosions or wrecked object replacements. For demonstration purposes, I’m instantiating my projectile prefab. I have a “rocketPrefab” serialized at the top of my code.
As you can see, I already have a prefab that I’m trying to instantiate. Once you have yours set up, we can get started.
What does Unity Instantiate do?
Instantiate let’s you call something into your game. It’s almost always used to call a GameObject or Component. Instantiate doesn’t actually call the prefab, but creates a copy and calls that. This let’s you rotate the prefab, and/ or change the position.
Unity Instantiate is perfect for projectiles, as it’s the simplest way to call them into the game. If you pair it with “Invoke Repeating” or a “Coroutine,” it works incredibly well. Using these the right way will let you instantiate every few seconds / frames / whenever you need.
Use Invoke repeating if you want to Instantiate every few seconds, and use a Coroutine for anything more complicated than that. Otherwise, if you only want to Instantiate once, you don’t need to use either.
How Do I Instantiate?
To Instantiate, first you usually want to set it equal to a variable. In this case, anything I instantiate, is a “GameObject laser”. Now in the future I can just use laser when calling Instantiated objects.
Next, you want to write out the GameObject you Serialized earlier. For me this is rocketPrefab. Next you want to use a comma, and write out what position you want your game object to be instantialized in.
A lot of times, this will either be a new vector 3, or it will just be transform.position, like in my case.
Next, you want to have another comma, than put the wanted rotation of what you’re bringing into your game. For most cases, Quarternion.identity is the only thing you will need to put. Of course, if this doesn’t work for your game you can feel free to play around with this setting.
Unity Instantiate Review
All together, the code looks like this. Starting by setting the Instantiated object equal to GameObject (or to a component), you than Instantiate the Serialized GO/ Component from the top of your script.
After that you write out the transform and rotation of your prefab separated by commas.
Note: You don’t need “as GameObject” as you’ve already set it equal to a gameobject.
What can I do after instantiating?
There are many things you can do after instantiating in unity. One of the most common things is adding velocity to add movement. You do this by getting the rigidbody you set your instantiation to.
Like the code above, you type it as gameobject.GetComponent<Rigidbody>(). For many people this will be Rigidbody3d, but I’m making a 2d game so it’s Rigidbody2d. Before this, make sure you have a rigidbody attached to your Prefab.
From here it’s “.velocity = new Vector2(0, projectileSpeed);”. The 0 is for moving x, and the prjectileSpeed (which I have as a serializable field at the top of my code) is for the y. In this case, the projectile is moving up at a speed of “projectileSpeed”.
How do I destroy the instantiated object?
From my code above, you can see I also destroyed every instantiated object. I went about this in the simplest possible way, which was just by adding Destroy(GameObject, Seconds).
There are many more complicated ways, but if you don’t need the object after a certain amount of seconds, I will always recommend this way.
Bonus: Another example of how to use Instantiating from my code: