Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FlofYT · Apr 01 at 07:34 PM · stringfloatintparsing errorconverting-var-type-to-other-var-type

String can't be converted to float or int?

Hello,

Im working on a small side project for fun right now, but with this issue, it isnt fun anymore. I want to create a console where you can program simple commands like Rotate [angle] and Move.


The problem is, i cant convert my extracted argument from the console to an intager or float.

I've tried everything from Parsing to Converting, Casting and i even wrote an loop that loops through all possible values and compares them to the string if one is the right number and returns it, but it just dosnt work with no real reason

i have been reading a lot through other answers that explain i need some Culture info or something but that didn't work either. .


Player movement:

 public class PlayerController : MonoBehaviour
 {
     public Animator anim;
     public float playerMoveStep = 1.2f;
 
     private void Awake()
     {
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
 
         print("Timer Started..");
         StartCoroutine("timer");
     }
 
     public void Move()
     {
         transform.position += transform.forward * playerMoveStep;
         anim.Play("Player_MOVE");
     }
 
     public void RotatePlayer(string sAngle)
     {
         int angle = int.Parse(sAngle);
         transform.rotation = Quaternion.Euler(0, angle, 0);
     }
 
     IEnumerator timer()
     {
         yield return new WaitForSeconds(1f);
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
         print("Timer ended");
     }
 
 }
 



Console System:

 public class CommandConsole : MonoBehaviour
 {
     public TMP_InputField console;
 
     public string command;
     public string args;
 
     public void RunCode()
     {
         List<string> lines = new List<string>();
         lines = EnumerateLines(console.textComponent);
 
         StartCoroutine("RunLines", lines);
     }
 
     List<string> EnumerateLines(TMP_Text text)
     {
         TMP_TextInfo textInfo = text.GetTextInfo(text.text);
         List<string> lines = new List<string>();
 
         for(int i = 0; i < textInfo.lineCount; i++)
         {
             TMP_LineInfo line = textInfo.lineInfo[i];
             lines.Add(text.text.Substring(line.firstCharacterIndex, line.characterCount));
         }
         return lines;
     }
     
     void MovePlayer()
     {
         PlayerController player = GameObject.FindObjectOfType<PlayerController>();
         player.GetComponent<PlayerController>().Move();
         print("Moving player...");
     }
 
     void RotatePlayer(string angle)
     {
         PlayerController player = GameObject.FindObjectOfType<PlayerController>();
         player.GetComponent<PlayerController>().RotatePlayer(angle);
         print("Rotating player..");
     }
 
     IEnumerator RunLines(List<string> lines)
     {
         
         for (int i = 0; i < lines.Count; i++)
         {
             string _command = lines[i];
             _command = _command.Split(' ')[0];
             command = _command;
 
 
             if (_command.Contains("Move"))
             {
                 Debug.Log(_command);
                 MovePlayer();
             }
 
             if (_command.Contains("Rotate"))
             {
                 args = lines[i].Split(' ')[1];
                 Debug.Log(_command);
                 RotatePlayer(lines[i].Split(' ')[1].ToString());
             }
 
             yield return new WaitForSeconds(0.5f);
         }
     }
 }
 

I would really appreachiate it, if you could answer this question because i am really struggeling with it. (I even copied some code from another project where this float parsing works and even that didnt work)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 01 at 09:30 PM

Well we can't really do much on our side. Have you actually checked if you have any additional characters in the string you pass to int.Parse? Any non digit character would throw the parser off. This could also be any non visible characters like a leading or trailing space, tab or newline character.


So the first thing you should check is what is actually passed to your "RotatePlayer" method. Try adding a Debug.Log statement like this to your method:

 Debug.Log("RotatePlayer: >"+sAngle+"< ("+sAngle.Length+")");

See if the character count matches the actual characters you expect. Another thing one could do is disecting the string into ascii bytes or utf8 bytes and printing out those to see if there's anything odd.


Another thing that a lot people forget that int.Parse and float.Parse by default use the local culture settings which may throw things off depending on the local culture settings. While this is usually much more important for floating point numbers, it may even have an effect on integers.

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

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

135 People are following this question.

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

Related Questions

Convert Text to float 3 Answers

what the hell is a int? (or a float, or boolean, or string!) 3 Answers

Can I create a list with an int/float and a string? C# 2 Answers

Can a player change (int, string, float etc....) from outside the game? 3 Answers

GUI.Label shows volume from a slider 3 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