Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Limit the Input

Discussion in 'Scripting' started by jerex, Aug 12, 2014.

  1. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    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.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Text.RegularExpressions;
    4. using System.Collections;
    5. public class Bed : MonoBehaviour {
    6.  
    7.     public static string bed="";
    8.  
    9.     void OnGUI(){
    10.         bed = GUI.TextField (new Rect (400, 260,50, 25), bed);
    11.         bed = Regex.Replace (bed, "[^0-9]", "");
    12.      
    13.         if(!CheckInput(bed))
    14.         {
    15.             GUI.Label(new Rect (400, 290,50, 25), "Wrong 'bed': " + bed);
    16.         }
    17.     }
    18.  
    19.     /// This method will check correcness of input
    20.     /// It is not well optimised, because it will call everytime when OnGUI called, so it could be improved
    21.     bool CheckInput(string data)
    22.     {
    23.         bool result = false;
    24.      
    25.         int convertedData = 0;
    26.         if(Int32.TryParse(data, out convertedData))
    27.         {
    28.             if(convertedData > 0 & convertedData <= 5)
    29.                 result = true;
    30.         }
    31.         return result;
    32.     }
    33. }
    34.  
    Here's the GUI Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Third : MonoBehaviour {
    5.    
    6.     public Texture background;
    7.     public GUIStyle Back;
    8.     public GUIStyle Next;
    9.     public float guiPlacementX1;
    10.     public float guiPlacementY1;
    11.     public float guiPlacementX2;
    12.     public float guiPlacementY2;
    13.    
    14.    
    15.    
    16.     void OnGUI (){
    17.        
    18.         GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), background);
    19.        
    20.         if(GUI.Button(new Rect(Screen.width* guiPlacementX1, Screen.height*guiPlacementY1, Screen.width*.2f, Screen.height*.15f),"", Next)){
    21.             Application.LoadLevel("Table");  
    22.         }
    23.        
    24.         if(GUI.Button(new Rect(Screen.width* guiPlacementX2, Screen.height*guiPlacementY2, Screen.width*.2f, Screen.height*.15f),"", Back)){
    25.             Application.LoadLevel("Chair");
    26.         }
    27.        
    28.     }
    29. }
    30.  
     
    Last edited: Aug 12, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    What's the error message?
     
  3. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    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.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    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.
     
  5. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    the user must not go to the next scene if the input is wrong. how to do that?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    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?
     
  7. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    I change the question and code kindly check it sir. :)
     
  8. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    I really can't understand what are you trying to do. can you explain?
     
    TheSniperFan likes this.
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    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?