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 Uriel_96 · Oct 31, 2010 at 04:00 AM · gamevariableclicktextfieldnumber

How to make a variable int having data of a textfield?

In my game I have a textfield that it has a number but what I want to do are several things. First I want to make that what you write in the textfield is the same as a variable that handles only numbers (int), also another thing is that if it is possible that in the textfield only can write numbers, and finally and very important, if I can do at the moment of do a clic in the textfield the game stop what he was doing, so if for example the game use for something the number 1 and you write in the textfield 1 nothing happens in the game.

I really need your answers so please if you know please answer. Thanks for future. :P

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
2
Best Answer

Answer by lhk · Oct 31, 2010 at 10:26 AM

To get the value of the textfield try (c#)

int number=int.Parse(textfield.text);

Restricting a textfield to numbers is difficult. I would try this (but it certainly isn't very good code)

try
{
    int number=int.Parse(textfield.text);    
}
catch(Exception e) 
{
    textfield.text="0"
}

This code checks if it can parse the text to a number. If the convertion fails, the textfield didn't contain a number and is set to 0. Please do not comment about this, I know this is very bad style. I also know the sentence "Don't use exceptions for flowcontrol"

To disable parts of your game I would add public variables to the script that checks the textfield. This should work (c#)

using UnityEngine;
using System.Collections;
public class textdisable : MonoBehaviour
{
    public GameObject object1;
    public GameObject object2;
    public GUIText textfield;
    int number;
    // Use this for initialization
    void Start ()
    {
    }
    // Update is called once per frame
    void Update ()
    {
         if(Input.GetButtonDown("Enter")){
        try
        {
            number=int.Parse(textfield.text);   
        }
        catch(Exception e)
        {
            textfield.text="0";
        }
        if(number==1)object1.active=false;
        if(number==11)object1.active=true;
        if(number==2)object2.active=false;
        if(number==22)object2.active=true;
        }
    }
}

In the inspector you can assign objects to the variables object1 and object2 of your script. If the textfield contains specific numbers the GameObjects object1 or object2 are enabled/disabled. Lets say object1 contains the script that spawns the enemies in your scene. When the user types 1 in the textfield and presses "Enter" the object is deactivated and no more enemies are spawned.

Comment
Add comment · Show 3 · 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 Uriel_96 · Oct 31, 2010 at 02:14 PM 0
Share

but you know how to do this in java script?

avatar image Uriel_96 · Oct 31, 2010 at 02:45 PM 0
Share

its there a way to deactivate the keys of the game when you are writing on the textfield, for example if the game use the key 1 to do something and the user is writing in a textfield and write the number 1 I dont want the game to react to that key number 1 at that moment

avatar image stfx · Mar 22, 2012 at 09:42 AM 0
Share

int.TryParse is the way to properly parse it without exception. See http://answers.unity3d.com/questions/40103/text-field-inputting-numbers-only.html

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

No one has followed this question yet.

Related Questions

How to output variable in textfield? 1 Answer

Edit Variables From Game! 1 Answer

how do I access these game objects by number? 2 Answers

How could I convert the y axis in a Vector 3 into a float? 1 Answer

I can't change a variable that is in another script. 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