Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't Convert String to float.

Discussion in 'Scripting' started by Epic-Username, Jul 19, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I'm trying to change a string into a float but it keeps giving me an error here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class INPUT : MonoBehaviour {
    6.  
    7.     public Text text;
    8.     public Controller c;
    9.  
    10.     void Start () {
    11.    
    12.     }
    13.  
    14.     void Update () {
    15.         if (Input.GetKey ("space"))
    16.             c.acceleration = float.Parse(text.ToString());
    17.     }
    18. }
    19.  
    But it keeps giving my this error when i enter a number:

    FormatException: Unknown char: T
    System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:209)
    System.Single.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Single.cs:183)
    INPUT.Update () (at Assets/Scripts/INPUT.cs:16)

    Why is this happening and how do i convert a string to float?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,511
    Looks like your text member is a Text, not an actual string. Have you tried text.text instead?
     
    Kiwasi likes this.
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Thanks it worked.
     
  6. unity_JLkWjdZ7NIsgkg

    unity_JLkWjdZ7NIsgkg

    Joined:
    Jan 14, 2021
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement: MonoBehaviour
    6. {
    7.     public float speed = 6f;
    8.     public float gravity = 20f;
    9.     public float jump = 8f;
    10.  
    11.     CharacterController Controller;
    12.  
    13.     Vector3 moveDirection = Vector3.zero;
    14.  
    15.     float Horizontal;
    16.     float Vertical;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         Controller = GetComponent<CharacterController>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         Horizontal = Input.GetAxis("Horizontal");
    27.         Vertical = Input.GetAxis("Vertical");
    28.         if (Controller.isGrounded)
    29.         {
    30.             moveDirection = new Vector3("Horizontal",0.0f,"Vertical");
    31.             moveDirection *= speed;
    32.         }
    33.  
    34.         moveDirection.y -= gravity * Time.deltaTime;
    35.         Controller.Move(moveDirection * Time.deltaTime);
    36.     }
    37. }
    i have a problem from he
    sorry i speaken't English
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    That's a necro and a half, seems to me, like you didn't really want the "" around your Horizontal and Vertical on line 30