Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mayk1993 · Sep 23, 2011 at 10:15 PM · javascriptinputnullreferenceexceptionmath

Help with eval()

So, I asked a similar question the other day, related to the same subject, asking how could I, as a general concept, input a string in my code, and someone told me I should look into the eval() function. So I did. I rewrote my code from C# into Java, and now I have:

 var a : String = null;
 var b : String = null;
 var z : int;
 var law : String;
 
 function Update () {
     System.Convert.ToInt32(a);
     System.Convert.ToInt32(b);
     z = eval(law);
 }
 
 function OnGUI() {
         GUI.Label(new Rect(300, 10, 200, 20), "a");
         GUI.Label(new Rect(300, 30, 200, 20), "b");
         GUI.Label(new Rect(300, 50, 200, 20), "law");
         a = GUI.TextField(new Rect(10, 10, 200, 20), a, 25);
         b = GUI.TextField(new Rect(10, 30, 200, 20), b, 25);
         law = GUI.TextField(new Rect(10, 50, 200, 20), law, 25);
         
         gameObject.GetComponent(GUIText).text = System.Convert.ToString(z);
         }



My problem is I get these errors:

1.

FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) System.Convert.ToInt32 (System.String value) Kalculator.Update () (at Assets/Kalculator.js:9)

2.

NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) Kalculator.Update () (at Assets/Kalculator.js:11)

3.

InvalidCastException: Cannot cast from source type to destination type. Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) Kalculator.Update () (at Assets/Kalculator.js:11)

4.

CompilationErrorsException: script(1,2): BCE0043: Boo.Lang.Compiler.CompilerError: Unexpected token: +. ---> script:1:2: unexpected token: ["+",,line=1,col=2] --- End of > inner exception stack trace --- > > UnityScript.Scripting.Evaluator.DoCompile > () > UnityScript.Scripting.Evaluator.CompileScript > () UnityScript.Scripting.Evaluator.Run > () > UnityScript.Scripting.Evaluator.Eval > (UnityScript.Scripting.EvaluationContext > context, System.String code) > Kalculator.Update () (at > Assets/Kalculator.js:11)

I also tried int.parseInt(a)/(b) instead of System.Convert, I get the same errors. Any help is gladly welcome.

Maybe I am doing this totally wrong. What I'm trying to do is input 3 strings: a, b, and law. a and b are ints and law is what they do. For example, if I were to input:

a=3 b=5 law=a*b + 1

I should get 16. Anyways, I'm really interested in how this can be implemented. Thank you in advance for all your help.

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 Benproductions1 · Apr 28, 2014 at 05:18 AM 1
Share

Ins$$anonymous$$d of using eval, which would result in horribly insecure code, you should parse your input.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

calculate terms in runtime 3 Answers

Need help using GUI.Button/ input storage 1 Answer

Press multiple switches to unlock the door 0 Answers

How to get the key from the button name 1 Answer

Why doesn't this do anything? 1 Answer


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