Hi everyone,

I’ve made my vehicle able to select 2 types of rockets based on whether I press “1” or “2” (and space bar to shoot). Except I wish to add more types of projectiles later on and I know the code will definitely get messy without an array, which I am currently not using. How may I implement projectile selection through an array? I know how to make an array for the projectiles, but I can’t manage to reference the projectile inside the array into the Instantiation script.

I appreciate your help- Hyperion

I’m not sure where you have the stumbling block, so I’ll take a shot:

At the top of the file you can do:

var prefab : GameObject[];

You can click on the triangle to the left of the variable, set the size of the array, and then drag and drop your prefabs into the array.

To fire the projectile you can do something like:

function FireProjectile(index : int) {
  var go = Instantiate(prefab[index], transform.position, transform.rotation);
  go.rigidbody.AddForce(transform.forward * force_factor);
}

There are several ways to translate keys to specific values to pass into fireProjectile. One simple way would be to use a switch() statement.