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 ashjack · Oct 15, 2013 at 05:15 PM · javascriptvariablenumbers

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.
 };
Comment
Add comment · Show 4 · 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
avatar image ashjack · Oct 15, 2013 at 08:02 PM 0
Share

Thanks! It really helped!

avatar image DylanW · Oct 16, 2013 at 05:53 AM 0
Share

Ah, don't worry about it! Glad I could help.

avatar image ashjack · Oct 16, 2013 at 04:21 PM 0
Share

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);}
avatar image fafase · Oct 17, 2013 at 08:02 AM 0
Share

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

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Resticing variables to whole numbers 3 Answers

Unity3d Javascript Reference Script Variable 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