Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 this 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

This 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;

this will make it a float literal instead of a double. Of course, this 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 this is a different MonoBehaviour script on your object. However, you also have a member "PlayerHealth", which is an integer!! This will cause serious confusion in your code, and you need to make sure that every name and token you use is unique (for exactly this 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 things in this 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

Please don't post comments as answers. Also, you should acknowledge the answer that helped you most by clicking the tick next to the answer to accept it.

avatar image EntropicJoey · Apr 12, 2012 at 08:19 AM 0
Share

i apologize for the misplacement! the script is working now thank you! and both posts helped me, solve and also to be informed of whats happening!

avatar image AlucardJay · Apr 12, 2012 at 08:35 AM 0
Share

@$$anonymous$$leptomaniac , you can use my Standard-New-User-Advice-Text if you like :P

I wonder if UnityAnswers could implement a procedure where if a person is writing in an answer field to their own question, force a check to confirm if this is just a comment or self-answer.

Text :

Hello. Did this script work, or did you find the suggestion useful? Please post a comment or mark an answer, for future reference by other people searching this 'site.

I wish this was made clearer for new users, so just some tips on using this 'site (for ALL new users) :

How to accept an answer :

On the left-hand-side of the Answer box , there are the following icons :

: Thumb Up : Number (of votes) : Thumb Down : A Tick :

If an answer worked for you , click on the 'Tick' , it should now be highlighted in green. If you like an answer on Any question , you can click on the Thumb UP , it should now be highlighted in green , and the number of votes should rise by 1.

How to reply to an answer / post a comment :

To make a comment , USE the 'add new comment' button, a window then opens to type in. The answer fields are for ANSWERS, so unless you are answering your own question , DON'T write in an answer box. This helps the 'site work properly, especially when other people are searching for answers, and want to read answers , not comments.

IF your question changes slightly while commenting and reading comments , EDIT the original Question, so anyone reading from the beginning knows what you are asking.

This will make for a happy experience for everyone. I made mistakes starting on this 'site too, but everyone is helpful if you learn and change these habits.

Following these simple steps helps the website work , and other readers to find answers also.

Happy Coding =]

the FAQ appears at the top of the page : http://answers.unity3d.com/page/faq.html

also : http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html

avatar image AlucardJay · Apr 12, 2012 at 08:39 AM 0
Share

this is no personal reflection on you @$$anonymous$$tropicJoey , I did exactly the same thing when I started =]

Happy Coding.

avatar image EntropicJoey · Apr 12, 2012 at 09:26 AM 0
Share

lol thank you very much for forum tips, the path i took was converting it from 0.5f to dividing it by 2, then even further removing out the divide and letting the number be represented by a changeable number in a different script, or atleast thats what i believe is going on! hope that will help who evers still stuck =o

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.

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

Guard pass throught the walls in Unity Project #1 Stealth! 1 Answer

Getting Error Object reference not set to an instance of an object 1 Answer

Lost files in project from unexpected shutdown? 2 Answers

How to Fix Compilier Errors? 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