Using Instantiate() to spawn objects

Now within this IF statement—meaning after the opening { and before the closing }, put the following line:

C#:

Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;

Javascript:

var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);

Here we are creating a new variable named instance. Into this variable we are storing a reference to the creation of a new object that is of type Rigidbody.

The Instantiate commands requires three pieces of information namely, Instantiate(What to make, Where to make it, a rotation to give it);

So in our example, we are telling our script to create an instance of whatever object or prefab is assigned to the bullet ...

Get Unity 3.x Game Development Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.