Limit the Input

Hi guys. I want to make that the user will not go to the next scene when the nextbutton is click. here’s my code in having an error message when the user input wrong format. i only accept 1-5 number if the user input higher or less than 1-5 error message will show.

using System;
using UnityEngine;
using System.Text.RegularExpressions;
using System.Collections;
public class Bed : MonoBehaviour {
  
    public static string bed="";
  
    void OnGUI(){
        bed = GUI.TextField (new Rect (400, 260,50, 25), bed);
        bed = Regex.Replace (bed, "[^0-9]", "");
      
        if(!CheckInput(bed))
        {
            GUI.Label(new Rect (400, 290,50, 25), "Wrong 'bed': " + bed);
        }
    }
  
    /// This method will check correcness of input
    /// It is not well optimised, because it will call everytime when OnGUI called, so it could be improved
    bool CheckInput(string data)
    {
        bool result = false;
      
        int convertedData = 0;
        if(Int32.TryParse(data, out convertedData))
        {
            if(convertedData > 0 & convertedData <= 5)
                result = true;
        }
        return result;
    }
}

Here’s the GUI Code:

using UnityEngine;
using System.Collections;

public class Third : MonoBehaviour {
   
    public Texture background;
    public GUIStyle Back;
    public GUIStyle Next;
    public float guiPlacementX1;
    public float guiPlacementY1;
    public float guiPlacementX2;
    public float guiPlacementY2;
   
   
   
    void OnGUI (){
       
        GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), background);
       
        if(GUI.Button(new Rect(Screen.width* guiPlacementX1, Screen.height*guiPlacementY1, Screen.width*.2f, Screen.height*.15f),"", Next)){
            Application.LoadLevel("Table");   
        }
       
        if(GUI.Button(new Rect(Screen.width* guiPlacementX2, Screen.height*guiPlacementY2, Screen.width*.2f, Screen.height*.15f),"", Back)){
            Application.LoadLevel("Chair");
        }
       
    }
}

What’s the error message?

I want to limit the input of user. there is no error to the code i gave. but there is a revision of code because example i want that the user will just only input 1-5 numbers but he/she input 6. the next button must not proceed to the next scene and it will display an error.

Oh, I thought you were saying that right now typing 6 caused a Unity error. You meant that you want it to display an error message.

You could probably use Event.current. I believe you can check against & modify Event.current.keyCode.

the user must not go to the next scene if the input is wrong. how to do that?

Oh, wait. Do you want to put limits on the number of digits they type? That is, you want to only allow them to type the numbers 1-99999?
If you’re already restricting the input to [0-9] in your test field, can’t you just check bed.Length?

I change the question and code kindly check it sir. :slight_smile:

I really can’t understand what are you trying to do. can you explain?

1 Like

Where you’ve got “Application.LoadLevel(…);”, wrap it in a conditional (“if (…) { … }”) block that checks that the answer is one where you want the user to go to the next level.

Is the code you’ve already got stuff you wrote yourself?