How to position something by a float in a new vector3 c#?

I have this but it doesnt allow it

Instantiate (FirstBall, new Vector3 (rigidbody2D.transform.position.x, rigidbody2D.transform.position.y + 0.5)), Quaternion.identity;

It doesn’t work because you don’t use a float but a double value. “0.5” is a double value. Adding a double to a float value will result in a double. You want to use a float literal instead:


Note: In C# floating point values without a suffix are doubles by default while in UnityScript (Unity’s Javascript) the default is float.

// In C#
0.5    // double
0.5f   // float
0.5d   // double

// In UnityScript
0.5    // float
0.5f   // float
0.5d   // double

So it’s recommended to always use the correct suffix :wink:

Just a simple coding error. You typed Vector3, which has 3 parts. But you only gave it two parts.

You can promise two and give two (I think) by changing the word to Vector2. Or can promise three and give three, by adding a dummy 0 for the third value.

It’s a little confusing, since sometimes the computer will take a mismatch. Like, I think Vector3 A = new Vector2(3,6); is legal. It assumes you meant (3,6,0) and adds the extra “,0” for you.