• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by rick-grendel · Jun 23, 2015 at 06:59 PM · c#errorfloat

error CS0266: Cannot implicitly convert type `double' to `float'. An explicit conversion exists (are you missing a cast?) whats wrong?

hello i am making a spawner with timers and this error popped up i dont get it so do you guys know the answers here is my script and the error is: error CS0266: Cannot implicitly convert type double' to float'. An explicit conversion exists (are you missing a cast?)

 using UnityEngine;
 using System.Collections;
 
 public class spawner : MonoBehaviour {
 
     public float WaitTime;
     public GameObject Box;
     public float DifChangeTime;
 
     private float Timer;
     private float DifTimer;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         Timer += Time.deltaTime;
         if (Timer > WaitTime) {
             transform.position = new Vector3 (Random.Range (197, 204), 30, Random.Range (155, 162));
             Timer = 0;
             Instantiate (Box, transform.position, Quaternion.identity);
         }
         if(DifTimer > DifChangeTime) {
             WaitTime = WaitTime - 0.5;
             DifTimer = 0;
         }
         if (WaitTime > 0.5) {
             DifTimer += Time.deltaTime;
         } else {
             DifTimer = 0;
         }
     }
 }

thanks for helping

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OctoMan · Jun 23, 2015 at 07:04 PM 0
Share

Not visible in the piece of code, try changing

 private double Another;

to

 private float Another;

cheers

avatar image rick-grendel · Jun 23, 2015 at 07:28 PM 0
Share

that isnt working but I will remove it thank you for the reply

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by fafase · Jun 23, 2015 at 07:29 PM

WaitTime is a float but 0.5 is a double. Both are not the same type so they cannot be compared.

0.5f with the f extension makes it s float. This is only necessary when using a dot.

  WaitTime = WaitTime - 0.5f;
  if (WaitTime < 0.5f)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rick-grendel · Jun 23, 2015 at 07:48 PM 0
Share

thank you!!!!!!

avatar image Bunny83 · Jun 23, 2015 at 08:00 PM 1
Share

Just want to add that you usually can compare a float to a double as the compiler is allowed to up-cast a float value to a double implicitly. What is not allowed is an implicit down case from double to float as float has less precision.

So the operation:

    WaitTime - 0.5;
 //  (float)  (double)

will result in a double value as the float value (WaitTime) will be automatically converted to double.

However when you assign this result back to WaitTime it will throw an error as you can't assign a double value to a float variable without an explicit cast.

Of course the comparison should also use a float literal value as the conversion to double is unnecessary and might cause other problems.

ps: The same rule applies for the types "int" and "float". "int" to "float" will be done implicitly but not reverse.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Float to int casting in C# script with modulo giving division by zero error 1 Answer

Quaternion.Slerp problem 2 Answers

Multiple Cars not working 1 Answer

How can I get correct figure from formula of float 1 Answer

public float being defined as object? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges