Simple bullet instantiation problem

Hello all,

I have this code:

var laser : Transform;

var fireRate : float = 0.5f;
private var nextFire : float = 0.0;

var speed : float = 1.0;

private var bottom : Vector3 = Vector3(0, -1, 0);

function Update() {

if (Input.GetButton("Fire1") && Time.time > nextFire) {

    nextFire = Time.time + fireRate;

    var clone : GameObject = Instantiate(laser, transform.position, transform.rotation) as GameObject;
    clone.transform.Translate(bottom * speed * Time.deltaTime);
    

}
}

I get an error at the last line “object reference not set to an instance of an object”. I have no idea why.

By the way, I have seen some tutorials using Rigidbody instead of transform and gameobject, but I guess that’s because the bullet is meant to be “invisible”. I want mine to be a normal object.

It looks like the clone variable is being set to null (hence the error), because the first parameter of the Instantiate() function is not a GameObject. The laser variable is declared as Transform instead. You probably want to do two things with it: turn it into a GameObject, and set its value to a specific prefab of your bullet.