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 Daniel_Slivinskiy · May 06, 2015 at 08:21 PM · javascriptmath

Int To Checkable String?

I am making a game (for my school) about math. It is displaying ints(which are randomly generated) + "" using GUI.Box. But it doens't work when i try to check stringToEdit for the int + "" in it. Here's the (JavaScript) code:

  private var stringToEdit : String = "";
  var min = 1;
 var max = 10;
 var first = Random.Range(1,10); 
 var second = "+";
 var third = Random.Range(1,10); 
 var fourth = "=";
 var fifth = first + third + "";
 var Random1 : GUIStyle;
 var textureToDisplay : Texture2D;
 var points = "";
         
         
 
 
 function Start(){
     first = Random.Range(1,10);
     third = Random.Range(1,10);
     }
 
  function OnGUI () {
      
         GUI.Box (Rect (410, 200, 10, 20), " " + first, Random1);
         GUI.Box (Rect (510, 200, 10, 20), " " + second, Random1);
         GUI.Box (Rect (610, 200, 10, 20), " " + third, Random1);
         GUI.Box (Rect (710, 200, 10, 20), " " + fourth, Random1);
         GUI.Box (Rect (810, 300, 10, 20), " " + points, Random1);
         GUI.Label (Rect (780, 180, textureToDisplay.width/2, textureToDisplay.height/4),
             textureToDisplay);
      
          stringToEdit = GUI.TextField (Rect (780, 200, 100, 100), stringToEdit, 2, Random1);
 
  }
             
 
  function Update () {
      
   
     if(stringToEdit == " " + fifth) { 
     Application.LoadLevel("Beginner");
          
 }
 } 




Thanks to anyone who will anwser.

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 NoseKills · May 06, 2015 at 08:24 PM 0
Share

You are calculating the value of fifth (the answer) when you initialize that variable.

After that in Start() you randomize new values for first and third. So at least one issue is that the program expects an answer that is not the sum of the shown varibles

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · May 07, 2015 at 02:27 AM

im not fixing your script but i thought i would get you thinking in the right direction. your problem is that anything in string format has no mathmatical value or signifance to a script even if it is a number.

this is also true with plus and equal signs. there is a function to attempt to convert a String to a real Int or Float so you can do math. i think you are looking for the Parse funtion. here is a simple example.

 var iamastring:String;
 var getrealnumber:int;
 
 iamastring="248";
 
 int.TryParse(iamastring, getrealnumber); 
 
 getnumber=getnumber*2;
 
 print("here is a number we can actually multiply"+getnumber);

as far as i know the "Parse" function does not recognize + or * or whatever symbols to add or whatever. but there are always ways. hehe. here is another creative example.

  var words:String;
  var arrayofwords:String[];
 
  var realnumber:int;
  var anotherrealnumber:int;
  
  words="943*385";
 
 
  if(words.Contains("*")){
          arrayofwords = words.Split("*"[0]);
          int.TryParse(arrayofwords[0],realnumber);
          int.TryParse(arrayofwords[1],anotherrealnumber);                     
  
          realnumber=realnumber*anotherrealnumber;
  
  print("here is a your number "+realnumber);}

this script will multiply if a string has a "*" sign in it. im not answering your question but i thought i would get you thinking in the right direction so you can figure it out yourself! Hehe. lookup string functions. like tryparce, split and contains. its good stuff to know and good at if you want to be a good scripter.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

getting the lowest point of a sine wave in javascript 3 Answers

Having random operators in an equation? 1 Answer

Increase speed of vehicle - math question (javascript) 3 Answers

power of problem java 2 Answers

Javascript Maths 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