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");
}
}
}