Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Convert string to float

Discussion in 'Scripting' started by Deleted User, Apr 15, 2013.

  1. Deleted User

    Deleted User

    Guest

    Hi to all, i would like to know how to convert a string that contain a Mathf functions (ex : Mathf.Sin(Mathf.Pow(1, 2.0) + Mathf.Pow(2, 2.0))) to Javascript language, without incurring in this error :

    "FormatException : Unknown char: M"

    P. S = i have tried with : "float.Parse()".

    Thanks.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Are you trying to convert a formula string into a float?

    so "Mathf.Sin(Mathf.Pow(1, 2.0) + Mathf.Pow(2, 2.0)))" would result in say 8 or whatever the result is?
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you're trying to run that string as code, you need to use eval().

    --Eric
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    How does it work???
    I searched for the eval function and found a massive wall of forum posts / QA posts that are saying its not possible because C# is a complied language and not a interpreter one.

    If it works how?
     
  5. Deleted User

    Deleted User

    Guest

    Yes i tried it, but Unity give me this error :


    UnityScript.Scripting.IEvalutationDomainProvider' interface member implementation must be public or explicit

    My intention is that write more formulas like that in my first comment, and convert these to float (ex : graph.tk), but without convert string into JS i can't make this.
     
    Last edited by a moderator: Apr 15, 2013
  6. Deleted User

    Deleted User

    Guest

    Ok guys, i have resolved eval() error, with delete all scripts contain "#pragma strict"

    Thanks for help :D
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's a Unityscript feature, not C#. It works by recompiling the code on the fly, so there's a noticeable pause when code is compiled the first time.

    (And yes, it doesn't work with #pragma strict, which also means it doesn't work on iOS.)

    --Eric