Why won't my crate fire?

OK I’m a newbie so go easy…

I have this code that I’m using while I’m following a tutorial on Unity3d but it seems that the code they are using doesn’t work in the latest version of unity or something because when I attach it to my Main camera nothing happens…

var speed = 3.0;

var cratePrefab:Transform;

function update () 
{
//when you press the button

if (Input.GetButtonDown ("Fire1"))
{
 //Create Crate
 var crate = Instantiate (cratePrefab, transform.position, Quaternion.identity);
 //Add force to shoot it
 crate.rigidbody.AddForce (transform.forward * 2000);
 }
}

this is supposed to shoot cubes into the 3d world but when I left click nothing happens…
Anyone want to help a newbie out?

Have you ensured the rigidbody in your prefab you target can handle forces?

Means

  • Mass should be greater 0
  • “Is Kinematic” NOT ticked
  • “Use Gravity” NOT ticked (<- this shouldn’t be the problem, but I guess you don’t want bulletdrop)

function Update ()

You need to use Update not update :slight_smile:

var cratePrefab:Transform;

should be :

var cratePrefab : GameObject;