C# Instantiate in Unity! – Beginner Scripting Tutorial


– Instantiate is a function which is used to create clones of GameObjects. This is commonly used in the
context of cloning a prefab. A prefab is simply a preconfigured object which is saved in the project’s assets. One such example might be firing projectiles from a launcher. Each of the projectiles would need to be Instantiated into the game world so that it could be fired. In this example, we’re using Fire1 to trigger an Instantiate function. The most basic form of
Instantiate simply takes one parameter, the object
that we wish to clone. In this example, we have created a public variable called projectile, and we’re passing this in
to the Instantiate function. However, this means
that the prefab will be Instantiated at its default position which, in this case, is zero. And in our example, that position is the center of the world and the center of where the launcher is at the moment. So, when we press Play, those projectiles appear at the center of the launcher, which is not what we want. Instead, we wanna fire
it like a projectile so it needs to appear in front of the opening of our launcher. For this, we need a slightly different version of Instantiate. This version of Instantiate
takes three parameters: the object to be
Instantiated, in our case, the projectile prefab,
and then, a position and rotation to give to our
new clone of the prefab. For this, I created an
empty GameObject that’s been positioned in front
of the launcher’s barrel. We’re using this by
creating a public variable called barrelEnd and we’ll
use the GameObject’s position and rotation values to assign to the new clone of our projectile prefab. By dragging and dropping
this new GameObject into my barrelEnd
variable in the Inspector, I can reference the transformed position and rotation of the empty
object and use those to assign to my projectile. So, when I press Play and press Fire, the projectile appears in the right place, but it just kind of drops,
which is not what we want. We need to be able to
affect the object that’s created by our instantiation, in this case the clone of the projectile. Normally, Instantiate
returns a type called object, but in order to fire our
projectile and add force to it, we’re gonna cast that type to a Rigidbody. To do this, we’re gonna add as Rigidbody after the Instantiate call, and then store the returned value in a Rigidbody variable, which we’ll call projectileInstance. So here, we set up a Rigidbody variable called projectileInstance
and we set that variable to be equal to the object returned by our Instantiate function. Now that we have a Rigidbody, we can use our variable
projectileInstance and address anything within the Rigidbody class. For example, AddForce. And again, we can make
use of the transformed barrelEnd to grab the
direction at which we want to AddForce. We can then multiply by a certain amount, so when I press Play and Fire, my projectiles are fired off by adding a force to the clone that I create. Bear in mind that when
you create many clones within the game, those things will still exist within your scene. So, if you’re doing something like this, you may wanna consider writing a script which removes them from the world after a certain amount of time. Here, my prefab has a
ProjectileDestruction script, which simply removes the object after a given number of seconds.

22 thoughts on “C# Instantiate in Unity! – Beginner Scripting Tutorial

  1. I would like that Unity would also offer a version of GetButtonDown that takes an enum (integer) instead of a string. It doesn't seem very efficient to me to scan a string for each inspection of a button press.

  2. Using instantiate with the fire button, something that will be used all the time…
    Isn't that not best practice? Doesn't instantiate create garbage for the collector?
    When using projectiles or something that is used all the time, isn't it better to have an array with objects in already and go through that
    instead of creating the prefab and then destroying it?

  3. Yeah for any beginners watching, don't always rely on Destroy to get rid of your projectiles. Research "Object Pooling" since this way once an object is created and used, it can be deactivated and re-used; if you just Destroy after one use this will create more garbage for garbage collection (something you want to keep to a minimum).

  4. so if i wanted to make say an arrow, i could make it shoot from the bow by instantiating it and giving it motion, attaching a damage script to it and making it so when it hits a colider of another object a destroy script removes the damage so it dosent keep damaging things and resets the velocity to 0. then i could have it set itself as a child of the object it hit so it moves with it. and then have a destroy on countdown to remove itself. is that about right? or am i making it too complicated.

  5. Some of this doesn't make sense. The projectile property is declared as type Rigidbody, but aren't prefabs usually of type GameObject? At least those used by beginners? How are you passing the prefab into the projectile field?

    Moreover, the line of code where you instantiate a Rigidbody to add force to will ONLY work if you have your project property typed as Rigidbody. While the instantiate method takes a Rigidbody parameter, there is apparently no implicit unboxing conversion between the two type (GameObject and Rigidbody). So this example works only for Rigidbodys passed into it?

    Am I missing something?

  6. hi please
    what is "as" and it do?
    how it give position & rotation value?it attach a game object to barrelEnd.what was that

  7. the usual huge mistake in Unity's official tutorials: showing very complicated stuff pretending to look simple, and in the end, everything looks like a very confusing mess…

Leave a Reply

Your email address will not be published. Required fields are marked *