- Home /
Converting Variables
I havent scripted in C# in a long while... and i know its possible to fix these errors but im forgeting how exactly to do it.
Here are the errors
Assets/Brennan's Assets/Scripts/Player/Experience.cs(16,25): error CS0029: Cannot implicitly convert type `int' to `bool'
Assets/Brennan's Assets/Scripts/Player/Experience.cs(18,33): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
Assets/Brennan's Assets/Scripts/Player/Experience.cs(22,17): error CS0029: Cannot implicitly convert type
int' to
bool'
I do remember how to make an int show on a gui... so if anyone else stumbles upon this topic they can atleast see... so ill offer this to people...
GUI.Label(new Rect(10, 10, 10, 20), "Your Text =" + Your Var);
Thanks in advanced to those who looked at this and are trying to help me.
This was suggested by a unity answers member... here is the script.
using UnityEngine; using System.Collections;
public class Experience : MonoBehaviour {
public int maxLvl = 100; public int curLvl = 1; public int curExp = 0; public float XPadd; public int advExp = 100; public bool XPlock = false;
void Update() {
if(XPlock = false) {
if(curExp = advExp) {
curLvl += 1;
advExp *= XPadd;
}
}
if(curLvl = maxLvl)
XPlock = true;
}
void Start() {
XPadd = 3.14159f;
}
void OnGUI() {
GUI.Box(new Rect(10, 10, Screen.width / 2 / (advExp / curExp), 20), curExp + "/" + advExp);
}
}
You have the errors on line 16, 18, and 22. If you could add those lines of code to the question that would help.
Answer by huggz · Feb 19, 2011 at 05:23 PM
you need to add the appropriate cast to the variable's being referenced. For instance you have an int that you're trying to use as a bool
int myInt = 1;
bool hasVal = myInt; // won't work
what you need to to explicitly convert the int to a bool like so:
bool hasVal = System.Convert.ToBoolean(myInt);
Personally, though, I find this to not generally be a good idea. A better idea is to check myInt
for the appropriate values and assign the bool that way.
bool hasVal = (myInt > 0); // or whatever you're expecting your bool to represent.
The same format will work for your float to int conversion. Just make sure you're aware that converting a float to an integer looses information.
float x = 45.90394f;
int x2 = System.Convert.ToInt32(x);
Debug.Log(x2); // will yield 45
Answer by Peter G · Feb 19, 2011 at 06:34 PM
You can just use the cast operator:
sourceType obj1 = someValue;
destinationType obj2 = (destinationType)obj1;
Assuming sourceType can be cast into destination type such as float into integer. With built-in data types, you only need an explicit cast if information will be lost such as the decimal when converting a float into an int.
With custom classes, good programming says to require an explicit cast if you will lose data (and implicit otherwise), but that is up to the programmer when they define the class.
Answer by Noztradamuz · Feb 18, 2015 at 05:42 PM
Do not use System.Convert.Toint() or such. Instead use those for int
string myString = "30";
int myIntVariable;
int.tryParse(myString, out myIntVariable );
same for float (float.TryParse()), you MUST use the word OUT and theres no need to use myIntVariable = something, just use TryParse the method will set the variable in the OUT parameter, use tryParse because if your string is something like "123abd" that will throw an exception, but with the TryParse the system catches that and sets the int/float variable to Zero insted. NOTE: TryParse only accept String as the first parameter, an other fast method to convert float to int or int to float is to use explicit cast
int myIntVariable = (int)myFloatVariable;
float myFloatVariable = (float)myIntVariable;
you need to pay close attention to this line on your code,
GUI.Box(new Rect(10, 10, Screen.width / 2 / (advExp / curExp), 20), curExp + "/" + advExp);
at least one parameter in each operation must be float in order to get decimal number as the result of the operation, if you write
print(10/3);
im pretty sure it will print only "3" if you need to get "3.33333" you need to insert any float into the operation
print(10.00F/3);
that will do the job.
Your answer
Follow this Question
Related Questions
help with a random battle generator? 2 Answers
how to edit only specific part of text UI ? 2 Answers
GUI.Label shows volume from a slider 3 Answers
Convert String to TrueString 0 Answers
Converting string to boolean 2 Answers