Question by
hopper · Jan 29, 2016 at 03:50 PM ·
randomrandom.range
Random Number Generator Error
I have been reading up for a while on randomness and I decided to give it a shot for a project. I have built the random number generator and I have no errors in the console itself but overtime I hit the generate button it changes the display to read "0". Any suggestions?
Code: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class RandomGenerator : MonoBehaviour
{
public Text display;
public InputField minimum;
public InputField maximum;
private float min;
private float max;
private float random;
public void Randomize ()
{
float.TryParse (minimum.ToString (), out min); //convert input field to a float for use in the "Random.Range" function
float.TryParse (maximum.ToString (), out max); // ^ ^
random = Random.Range (min, max); //generate a random number
display.text = random.ToString(); //display a random number on a Text object
}
public void ResetVariables ()
{
minimum.text = "";
maximum.text = "";
display.text = "";
}
}
Comment
Answer by ThomasVandenberghe · Jan 29, 2016 at 04:05 PM
Try using
minimum.text.ToString()
maximum.text.ToString()
Currently you're trying to convert the inputfield object itself to a string instead of the text.