I have been able to dynamically change text of the GameComponent UI text objects using my C# script. Now I have been unable to find any way to get the current text of the text object into my script. Here is what I am trying to do. I am asking a series of questions to the user and each answer that can be selected has a button object added to it. When the user clicks the text I want the current value of that text to be passed to my script so I can check if that answer was the correct one.
Changing the text was easy because I just create the public variables and assign them in a GameObject. I wish when I clicked my text that I could pass that text current string to my Visual Studio script. See Below:
using UnityEngine.UI; //you need this to be able to access the Text Component
//i assume this is part of your code where you drag your TextObjects into
public GameObject Answer1;
//and this shall be your string where you want to save the answer.
private string yourAnswer;
//this might be the method you could bind to the button_click event
public void AnyFunction()
{
yourAnswer = Answer1.GetComponent<Text>().text;
}
Vothaka and Juice-Tin I appreciate everything. Looks like my problem now as you can see in the image that my GameObject is quiz and my script name is quiz.
The tutorials I followed said it was good practice but if I do that in my code for public GameObject Quiz the compiler complains.
there is a difference between the name of your script (which corresponds to the name of your class) and the name of the object you want to create (which would be Answer1)
so
class Quiz
{
public GameObject Quiz;
}
of course cannot work as the name Quiz is already given to the class itself
Makes sense. Fun learning something new. I just need to correct that on my part and I will post what I did. Other than that thank you all for your help!