O'Reilly logo

Unity 3.x Game Development Essentials by Will Goldstone

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required