- Home /
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);
}
}
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).
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;
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!!
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.
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!
@$$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
this is no personal reflection on you @$$anonymous$$tropicJoey , I did exactly the same thing when I started =]
Happy Coding.
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