Vector2 = new Vector2(floatX,flotY) - does not work

Can someone tell me, why, when I try to pass a variable to the Vector coordinates it does not work and I get error?

For example:

float howFast = 5f;

Vector2 speed = new Vector2(howFast,0f);

What do I do wrong? :frowning:

Well, if you have this code inside a class it will throw an error because both variables are member variables and you can’t use on variable in a fieldinitializer of another variable. Field initializers are executed in arbitrary order.

You have to move the initializarion of your speed variable to Awake or Start.

public class SomeClass : MonoBehaviour
{
    float howFast = 5.0f;
    Vector2 speed;   // declare here to make it a member variable of the class
    void Awake()
    {
        // initialize the variable here
        speed = new Vector2(howFast, 0.0f);
    }
}