- Home /
Error when colliding and calling a IEnumerator
So I have code here and in the console the error says:
- The best overload method match for 'UnityEngine.WaitForSeconds(float)' has some invalid arguments
- Argument '#1' cannot convert 'double' expression to type 'float'
public class FloatDown : MonoBehaviour {
public bool gravity = false;
// Use this for initialization
void Start () {
rigidbody2D.gravityScale = 0;
}
IEnumerator grav(){
if (gravity == true) {
yield return new WaitForSeconds(0.5);
rigidbody2D.gravityScale = 0;
gravity = false;
}
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Wall") {
rigidbody2D.gravityScale = 0.5f;
gravity = true;
StartCoroutine( grav() );
}
}
}
If you could help me, it would be very much appreciated. Thanks!
Answer by Kiwasi · Oct 15, 2014 at 11:28 PM
Line 11 needs an f after your number
yield return new WaitForSeconds(0.5f);
Right, in C# the default floating point type is "double" while in UnityScript it is "float".
//C#
var a = 0.5f; // a will be a float
var b = 0.5d; // b will be a double
var c = 0.5; // c will be a double since double is the default
//UnityScript
var a = 0.5f; // a will be a float
var b = 0.5d; // b will be a double
var c = 0.5; // c will be a float since float is the default
A float can be assigned to a double without any problems but the reverse doesn't work since a double is twice as large as a float. You would need to explicitly cast the double value to a float which will result in a loss of precission. Since the constructor of WaitForSeconds expects a float value you should simply pass a float ins$$anonymous$$d of a double.
Wow I'm stupid. I even had that earlier and I got rid of it cause I thought that was the error from before.