• 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
0
Question by EntropicJoey · Apr 12, 2012 at 12:26 AM · error3dconversion

Newb friendly error x.x conversion float to int

Assets/_Scripts/_Player/PlayerLevel.cs(33,18): error CS0266: Cannot implicitly convert type double' to int'. An explicit conversion exists (are you missing a cast?) i couldnt find a problem relative enough to mine in forums for me to fix t$$anonymous$$s myself please help

 using UnityEngine;
 using System.Collections;
 
 public class PlayerLevel : MonoBehaviour {
     
     private int curLevel = 1;
     private int maxlevel;
     
     public int curExp = 99;
     private int maxExp = 100;
     
     public float expBarLength;
     
     private int PlayerHealth;
     
     private bool LevelUp = false;
     
     void Start () 
     {
     expBarLength = Screen.width / 2;
     }
     void Update () 
     {
     PlayerHealth playerHealth = gameObject.GetComponent<PlayerHealth>();
     AdjustCurrentExp (0);
         
     if(curExp >= maxExp)
         {
     curExp = 1;
     curLevel++;
     maxExp += (20 * curLevel);
     LevelUp=true;
     playerHealth.endurance += (0.5 * curLevel);
         }
     }
     void OnGUI()
     {
         GUI.Box(new Rect(20, 50, expBarLength, 20), curExp + " / " + maxExp);
         GUI.Box(new Rect(20, 20, 80, 20), "Level: " + curLevel);
     }
     public void AdjustCurrentExp(int adjExp)
     {
         curExp  += adjExp;
         
         expBarLength = (Screen.width / 3 * curExp / (float)maxExp);
     }
 }
Comment
Add comment
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

3 Replies

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

Answer by syclamoth · Apr 12, 2012 at 12:33 AM

T$$anonymous$$s happens because when you write the number

 0.5 // or any other number of the form 'digits.digits'

the type of the number defaults to 'double'. If you instead want it to be treated as a float, you should use

 0.5f;

t$$anonymous$$s will make it a float literal instead of a double. Of course, t$$anonymous$$s isn't neccesarily the problem- I need to know what type 'PlayerHealth.endurance' is.

Looking through your code, I can see several serious issues with it. For starters, you seem to have a type "PlayerHealth"- I assume that t$$anonymous$$s is a different MonoBehaviour script on your object. However, you also have a member "PlayerHealth", w$$anonymous$$ch is an integer!! T$$anonymous$$s will cause serious confusion in your code, and you need to make sure that every name and token you use is unique (for exactly t$$anonymous$$s reason).

Comment
Add comment · 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
1

Answer by Eric5h5 · Apr 12, 2012 at 12:36 AM

Should be 0.5f, not 0.5. Although then you'd have to cast to int; you'd be better off just doing playerHealth.endurance += curLevel / 2;

Comment
Add comment · 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
0

Answer by EntropicJoey · Apr 12, 2012 at 07:44 AM

Wwell i was following tutorials i understand a bit of whats happening in scripts But then again not at all lol its tied into playerLevel (thats not part of what i understand lol, if itll help better Understand what some t$$anonymous$$ngs in t$$anonymous$$s script are ill post the Script, but first ill try both of your suggestions! Thanks very kindly!!

Comment
Add comment · Show 5 · 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 Kleptomaniac · Apr 12, 2012 at 07:47 AM 0
Share
avatar image EntropicJoey · Apr 12, 2012 at 08:19 AM 0
Share
avatar image AlucardJay · Apr 12, 2012 at 08:35 AM 0
Share
avatar image AlucardJay · Apr 12, 2012 at 08:39 AM 0
Share
avatar image EntropicJoey · Apr 12, 2012 at 09:26 AM 0
Share

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Error CS1519 Help 1 Answer

Why Do I Suddenly Have This Problem? 0 Answers

Does Unity Work With DAZ? 2 Answers

I am having problems uploading my project to Xcode Can anyone help??? 0 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