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 BBSA · Feb 19, 2011 at 03:22 PM · booleanfloatconvertcs0029cs0266

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);
 }

}

Comment
Add comment · Show 1
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 GesterX · Feb 19, 2011 at 04:27 PM 0
Share

You have the errors on line 16, 18, and 22. If you could add those lines of code to the question that would help.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

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

Comment
Add comment · Show 3 · 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 BBSA · Feb 19, 2011 at 07:48 PM 0
Share

did u write that in javascript?

avatar image huggz · Feb 20, 2011 at 01:43 AM 0
Share

nope. That's C#.

avatar image frodoe7 · Feb 18, 2015 at 04:55 PM 0
Share

What about converting int to float ?

avatar image
0

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.

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 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;
Comment
Add comment · Show 1 · 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 Noztradamuz · Feb 18, 2015 at 05:50 PM 0
Share

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

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

1 Person is following this question.

avatar image

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


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