how to convert text to string
I want to convert a UI Text to a string, so I can see if it is a correct answer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class car : MonoBehaviour {
public GameObject fordimage;
public GameObject chevyimage;
public GameObject jaguarimage;
public GameObject mazdaimage;
public GameObject toyotaimage;
public int counter;
public Text answer;
public string correctanswer;
public Button nextbutton;
void Start ()
{
correctanswer = ("");
fordimage.SetActive (false);
chevyimage.SetActive (false);
jaguarimage.SetActive (false);
mazdaimage.SetActive (false);
toyotaimage.SetActive (false);
counter = Random.Range (1,5);
nextbutton.interactable = false;
print (counter);
}
void Update ()
{
if (answer.ToString() == correctanswer) //error on this line
{
nextbutton.interactable = true;
}
if (counter == 1)
{
fordimage.SetActive (true);
correctanswer = ("ford");
}
else
{
fordimage.SetActive (false);
}
if (counter == 2)
{
chevyimage.SetActive (true);
correctanswer = ("chevy");
}
else
{
chevyimage.SetActive (false);
}
if (counter == 3)
{
jaguarimage.SetActive (true);
correctanswer = ("jaguar");
}
else
{
jaguarimage.SetActive (false);
}
if (counter == 4)
{
mazdaimage.SetActive (true);
correctanswer = ("mazda");
}
else
{
mazdaimage.SetActive (false);
}
if (counter == 5)
{
toyotaimage.SetActive (true);
correctanswer = ("toyota");
}
else
{
toyotaimage.SetActive (false);
}
}
}
So I have a input field, and I am trying to get the text that the player inputs.
Answer by JohnsonGames · Nov 10, 2017 at 02:41 PM
Found the Answer:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class car : MonoBehaviour {
public GameObject fordimage;
public GameObject chevyimage;
public GameObject jaguarimage;
public GameObject mazdaimage;
public GameObject toyotaimage;
public int counter;
public InputField answer;
public string correctanswer;
public Button nextbutton;
public Button pausebutton;
void Start ()
{
nextbutton.interactable = false;
correctanswer = ("None");
fordimage.SetActive (false);
chevyimage.SetActive (false);
jaguarimage.SetActive (false);
mazdaimage.SetActive (false);
toyotaimage.SetActive (false);
counter = Random.Range (1,6);
print (counter);
Button next1 = nextbutton.GetComponent<Button> ();
next1.onClick.AddListener (clicked);
}
void clicked ()
{
correctanswer = ("None");
counter = Random.Range (1,6);
nextbutton.interactable = false;
}
void Update ()
{
if (answer.text == correctanswer)
{
nextbutton.interactable = true;
}
if (counter == 1)
{
fordimage.SetActive (true);
correctanswer = ("ford");
}
else
{
fordimage.SetActive (false);
}
if (counter == 2)
{
chevyimage.SetActive (true);
correctanswer = ("chevy");
}
else
{
chevyimage.SetActive (false);
}
if (counter == 3)
{
jaguarimage.SetActive (true);
correctanswer = ("jaguar");
}
else
{
jaguarimage.SetActive (false);
}
if (counter == 4)
{
mazdaimage.SetActive (true);
correctanswer = ("mazda");
}
else
{
mazdaimage.SetActive (false);
}
if (counter == 5)
{
toyotaimage.SetActive (true);
correctanswer = ("toyota");
}
else
{
toyotaimage.SetActive (false);
}
}
}
You "Found the Answer"? You mean smkmth posted the answer 18 hours ago...
Answer by smkmth · Nov 09, 2017 at 10:48 PM
answer.text is how you access the text component of a text game object
To elaborate: UI Text is an object which handles the rendering of a string (because strings are just data that represent a sequence of characters, it doesn't care how it should display that data (like fonts, colour, etc.)). An instance of UI Text will store the string that it is responsible for rendering in the "text" property of the object, (which as noted in the answers, is accessible through [insert_name_of_ui_text_instance].text).
Answer by TarahPeltz · Nov 10, 2017 at 02:26 AM
@smkmth is correct, use answer.text. Here is the Unity Documentation for this: https://docs.unity3d.com/ScriptReference/UI.Text-text.html