- Home /
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.
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
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.
Your answer

Follow this Question
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