- Home /
Putting numbers on the end of variables via scripting
Hi again! Basically, I am making an interactive mobile phone for my game, and any mobile phone needs to be able to call numbers. I have the buttons 0-9 layed out, but have no idea how I will script it to remember which combination of numbers have been clicked. How could I do this in JavaScript? In case it helps, one of the callable numbers in my game is 01632960000.
Answer by DylanW · Oct 15, 2013 at 06:39 PM
If its a string, you can simply add any number to it. I have provided a script below to set an example, please read the comments:
var phoneNumber : String;
function Update()
{
// Check what key was pressed down.
if(Input.GetKeyDown("0")) {
// If 0, append it to phoneNumber.
phoneNumber += 0;
Debug.Log(phoneNumber);
}
else if(Input.GetKeyDown("1")) {
phoneNumber += 1;
Debug.Log(phoneNumber);
}
else if(Input.GetKeyDown("2")) {
phoneNumber += 2;
Debug.Log(phoneNumber);
}
// etc.
};
Two things, how can I limit the length of the string (in javascript)? I want it to have a maximum length of 11 characters. Also, I just tested the script, and added the following section, but it does not work, and no errors, warnings or logs come up.
else if(Input.Get$$anonymous$$eyDown("o")&& phoneNumber == 01632960000){
Debug.Log(phoneNumber);}
phoneNumber is a string so you need to use it as such:
else if(Input.Get$$anonymous$$eyDown("o")&& phoneNumber == "01632960000"){
Debug.Log(phoneNumber);}
Also "o" might not be defined, use $$anonymous$$eycode.O ins$$anonymous$$d.
And to limit then you need to count how many you already have and remove the extra. For instance, ins$$anonymous$$d of a string use a character array.
if(phoneNumber.Length > 11) {}
or
char [] limitedString = new Char[11];
But that involves extra care or you get an array out of bounds exception
Your answer
![](https://koobas.hobune.stream/wayback/20220613120000im_/https://answers.unity.com/themes/thub/images/avi.jpg)