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
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.