Question by
SoftwareEngineer · Dec 15, 2015 at 02:49 AM ·
c#unity 5input
How To Attach a Number To a Variable Which Is Got From User
I want to assign a number to a variable via getting input from user. For example:
print("Enter the maximum number: ");
How can I get number from user?
I know how to do in C# but Unity3d seems does not support it.
Comment
Answer by sandeepsmartest · Dec 15, 2015 at 04:50 AM
using UnityEngine;
using System.Collections;
using System;
public class NSKS : MonoBehaviour {
public string stringToNumber,Message="Message To Disp";
int AIntvariable;
void OnGUI() {
stringToNumber= GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
if (GUI.Button(new Rect(70, 250, 50, 50), "Click Me")){
try {
AIntvariable=int.Parse(stringToEdit);
if(AIntvariable>=0)
Message= "u have Entered"+stringToEdit.ToString();
}
catch (Exception e) {
Message ="Enter Numbers only";
}
}
GUI.Label(new Rect(Screen.width/2,Screen.height/2-200, 400, 20), Message);
}
}
Code is untested but should work. Hope this may help you.NSKS
Your answer
Follow this Question
Related Questions
Unity physics Help Implementing Real Life Physics in Jumping System C# 2 Answers
Is the Input System not working properly? (OSX Unity Editor 5.4.1) 2 Answers
Horizontal Input showing random decimal when it should be 0 0 Answers
Changing game controls for android 0 Answers
How to take user input, convert it integer, and print it on screen? 1 Answer