How to convert an inputfield's string to an int? (C#)
I'm trying to make an interactive random number generator, and I have two inputfields for the minimum and the maximum values. I know you can make a string to an int by doing int.Parse(variable), but if I do that Visual Studio gives me an error saying "Cannot implicitly convert type "int" to "UnityEngine.UI.Text". Below is my code if it helps.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Controller : MonoBehaviour {
public Text randomMin;
public Text randomMax;
private void Start()
{
randomMin = int.Parse(randomMin.text);
randomMax = int.Parse(randomMax.text);
}
void updateMin (int Minimum)
{
randomMin = Minimum;
}
void updateMax (int Maximum)
{
randomMax = Maximum;
}
}
Answer by UnityCoach · Feb 26, 2017 at 10:46 PM
randomMin is of type Text, which is is the component. You mean to assign its text property.
randomMin.text = int.Parse(randomMin.text);
randomMax.text = int.Parse(randomMax.text);
I was to quick to answer. In fact, you need to reference an int at some point. Like :
int $$anonymous$$ = int.Parse(random$$anonymous$$in.text);
Or if you just want to force the input to int, you may do :
random$$anonymous$$in.text = int.Parse(random$$anonymous$$in.text).ToString();
Or just use the content type parameter of the InputField.
And you want to reference an InputField, not just a Text.
Your answer
Follow this Question
Related Questions
Unity 5 doesn't detect some positions in Vector3 0 Answers
The Built [exe] of my game won't go past Splash Screen 0 Answers
[C#] The left-hand side of an assignment must be a variable, a property or an indexer 0 Answers
I have an error on a C# script @username 2 Answers
Nav Mesh Problem with SetDestination 1 Answer