How can i do this for my Unity App?
I'd like to create an app on unity which has just one main screen where you tap a button and it gives you random facts, i can write the facts and stuff but how do I make the button which switches between the facts and how do I put the facts in a queue or something? Please Help, Thanks! (C Sharp)
Answer by Jawchewa · Jun 13, 2017 at 09:03 PM
I would probably create a script that looks something like this:
public class RandomFact : MonoBehaviour {
public Text text;
public List<string> facts;
public void Click()
{
text.text = facts[Random.Range(0, facts.Count)];
}
}
Create a canvas with a button and a text object, and attach this script to the button. Then, in the editor, add this new click event to the OnClick event of the button. You'll also need to attach the text object from the canvas to the textfield in the script. Then, you should easily be able to populate the Facts list with all of the facts that you want.
Thanks for giving your time to help me. I will definitely try this out!
Also, what is the editor and how do i access it? Which one is the text field in the script and how do I put it in? Do I write the fact list in the script or in the canvas? Sorry I am a beginner to Unity. Thanks
Oh, by Editor, I just meant to set it in the Inspector panel inside of Unity. I would look at this page of documentation:
https://docs.unity3d.com/$$anonymous$$anual/script-Button.html
Basically, you need to go through Unity, and Create a new GameObject with a button. You can do this by going to the top toolbar and hitting GameObject->UI->Button. Also do the same thing for Creating a text GameObject by going GameObject->UI->Text. Then, attach the script that I wrote to the button, and drag the Text GameObject into the text field for that component. You can also populate the list of strings with all of the facts that that you want in the "facts" section on the RandomFact Script.
Then, to hook up the button, click the + button on the OnClick area of the button, and set the Object to be the button game object. Then, you will have to find the Click Event from the Random Fact script.
Reading through it, I don't know if this description helps or if it just makes it more complicated. Let me know if you need any other help.
Hi, I've just started to create the app. I've written your script into monodevelop and a lot of the word are 'incorrect' (underlined in red), @Jawchewa . Sorry about the hassle, I'm a noob.
Answer by xMEH · Jul 26, 2017 at 03:37 PM
Thanks a lot for the help! Just one more question sorry, do I write the random facts in the actual script or in the empty game object? @Jawchewa
Inside the inspector in Unity for the RandomFacts Script, there should be an area for "Facts" from where it was defined in the code. Inside this area, you should be able to add new strings that contains each of the facts.
@Jawchewa I fixed the script. I have attached the script to the button etc but for the on click event what do I put? Also, for the random facts script there are lots of 'elements', what do I type in there? Thanks
Drag the Random Fact Script into the field for the object. Then, In the function, Navigate to RandomFact->Click() and use that.
I'm not quite sure what you are referring to with elements. If you are just referring to the textboxes underneath the Facts list, then that is where you would type your Random Facts.