- Home /
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.
Answer by Jacek_Wesolowski · May 04, 2012 at 07:59 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate w/ Mouse Click: Random(?) Z.Axis problem (2D) 2 Answers
Trouble spawning cloned objects 1 Answer
Instantion not taking account of position variable. 1 Answer
How to Spawn and Place an Object in Line after Spawned Randomized Object? 0 Answers
Photon PUN, HTC Vive camera prefab over network instantiation 0 Answers