Search Unity

Cannot Convert Argument from Double to a float

Discussion in 'Scripting' started by MrHammy36, Apr 7, 2017.

Thread Status:
Not open for further replies.
  1. MrHammy36

    MrHammy36

    Joined:
    Apr 4, 2017
    Posts:
    31
    Hello I am trying to instantiate a player into a scene and my code is throwing the following error

    Error CS1503 Argument 1: cannot convert from 'double' to 'float' Pornstar Fighters D:\New folder\Pornstar Fighters\Assets\Scripts\ButtonAction.cs 18

    I am kind of lost here as well I am still kind of new to the API and learning, could any one possibly lead me in the right direction on how to fix this? Code is below.

    Code (csharp):
    1.  
    2. public void Character1()
    3.  {
    4.         player1 = GetComponent<GameObject>();
    5.   SceneManager.LoadScene ("Sky city scene lite", LoadSceneMode.Single);
    6.         Instantiate(player1, new Vector3(-40.10,23,44.51), Quaternion.identity);
    7.  
    8.  }
    9.  
     
  2. cdarklock

    cdarklock

    Joined:
    Jan 3, 2016
    Posts:
    455
    There are three kinds of decimal numbers in C#. There is the decimal, which is very slow but very precise; the double, which is much faster but not as precise; and the float, which is even faster and less precise.

    By default, if you just type the number -40.10 in your code, C# will give you a double. But Vector3() takes three float arguments, so you need to tell it you want a float.

    You do this with the letter f after your number: -40.10f

    If you wanted a decimal, you would use a letter m: -40.10m

    And if you wanted to very clearly state you wanted a double, you would use d: -40.10d

    Those last two also trip people up, because if -40.10 is a double, and you want a decimal... it only makes sense to say -40.10d, right? Wrong. -40.10m is the right way to declare a decimal constant. But neither of those matter much in Unity, because we use float for almost everything.

    So what you need is to change the Vector3 constructor call:

    Code (csharp):
    1.  
    2. new Vector3( -40.10f, 23f, 44.51f )
    3.  
     
  3. MrHammy36

    MrHammy36

    Joined:
    Apr 4, 2017
    Posts:
    31
    Thank you So Much cdarklock, That fixed it. I need to keep that in mind for next time ;)
     
Thread Status:
Not open for further replies.