Calw
Calw

Reputation: 45

Why can I not implicitly convert type 'UnityEngine.Vector2' to 'float'?

I'm trying to reset an object's position on my main menu screen without reloading the scene. However, I am getting this error (Cannot implicitly convert type 'UnityEngine.Vector2' to 'float') when attempting to reset the velocity and angular velocity of the falling object.

I have tried replacing the 'Vector2.zero' part to just '0f' and to a float that is equal to 0f but it also didn't work.

void Update () {
        transform.Translate (Vector2.right * Time.deltaTime * moveSpeed);
        if (player.transform.position.x >= 13f) {
            player.GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
            player.GetComponent<Rigidbody2D> ().angularVelocity = Vector2.zero;
            player.transform.position = new Vector2 (-8.5f, 6f);
        }
    }

Upvotes: 0

Views: 5702

Answers (1)

rutter
rutter

Reputation: 11452

Your problem is here:

player.GetComponent<Rigidbody2D>().angularVelocity = Vector2.zero;

angularVelocity is a float, not a vector. You're trying to pass a pair of values where the compiler can only use one, so it doesn't understand what you want to do.

You can assign 0f instead:

player.GetComponent<Rigidbody2D>().angularVelocity = 0f;

Upvotes: 5

Related Questions