Issue with adding velocity to the lazers

Hi,

When I try to run the code with the velocity as a publicly declared variable, the lazer gameobjects fail to gain that velocity.
However if I remove the variable, and in its place, just type 10f, then the lazers DO gain the velocity.

Here is the relevant code:
public float Projectilespeed = 10f;

void Update () {
    Vector3 startposition = transform.position + new Vector3(0, -1, 0);
    GameObject laser = Instantiate(projectile, startposition, Quaternion.identity) as GameObject;
    laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0f, -Projectilespeed);
}

Any solutions would be much appreciated,
Thanks :slight_smile:

If you remove the initialization (i.e.: =10f), have you tried modifying the value of the variable in the Inspector to 10?

The default value of a public variable, not initializated in a script, in the Inspector is always 0, so that may be your issue.

Privacy & Terms