[SOLVED] Player lasers won't move after being made

I can get the player’s lasers to instantiate upon pressing the spacebar, but they won’t fire upward. They just sit where they were instantiated. This is my code, inside of the Update method:

if (Input.GetKey(KeyCode.Space)) {
	GameObject laserInstance = Instantiate (laserPrefab, transform.position, Quaternion.identity) as GameObject;
	laserInstance.GetComponent<Rigidbody2D> ().velocity = new Vector3 (0, laserSpeed, 0);
}

I declare laserSpeed above it as this: public float laserSpeed = 5f;

I’ve stopped the game and looked at the lasers after they’ve been instantiated, and their velocity stays stubbornly at 0. I don’t get any console errors either.

Also, I’m using Unity 5, by the way.

EDIT: I fixed it by making the laserSpeed variable private and then public again. Something was reassigning it to 0 as soon as the game started (and no, it wasn’t the value it was set to publicly in the inspector; it was 5 there too).

Privacy & Terms