Quiz Game - Change Button text etc
Hey guys, some people asked similiar questions, but none of the answers fits to my needs.
At the moment, I created a Canvas > 4 Buttons > Text (each button got Text as child and the Buttons are children of Canvas)
But I seriously don't get Unity, I'm coming from libgdx. I really prefer the Script-method, but I think it's not possible that easy with Unity, is it?
So my plan was to load the scene and before displaying the buttons, I'll change the text of them dynamically. Usually, I would have a Class and in it, creating 4 buttons, giving them their textvalue with something like btn1.text() = "" and add an eventhandler which checks for clicks on certain buttonclicks.
But all tutorials I've seen, most of them create a script for each Button (srsly?), some of them parse parameters and use an eventhandler, but every button includes this script and it's also in the canvas somehow. Some use GetComponent(), but I cannot use it the way they're using it, it gives me a compile error if I don't include it in a function but in the global area of a class. And if I try one of these methods (still looking for the best way tbh), which scripts do I have to add to which game objects ? >.<
I am thinking about creating a Script EventHandler, adding it to Canvas-Gameobject. Then, I'll create 4 public Buttons in which I'll add my gameobject-buttons (although I don't like it doing this way, strange way to code, lol, would like to get the 4 buttons with a method like: getButtons, saving in a list or something which I could loop), these will have a parameter 1, 2, 3, 4 and they'll call the onClickFunction of my EventHandler, switching the int to know which button has been pressed. Still don't know if it will be easy to connect those numbers / button to the questions/answers though, but should work.
Is that way a good solution or could you give me a better idea? And I prefer using C# (although, is there any difference between using both, or will it be the same shit in the end?)
Thank you for your help.
Kind regards,
TehEbil
this is fairly straightforward thing to do - as you suggested - you can build most of it in the editor and use a single script to find the buttons, apply text, etc.
try posting the code you've used (including the errors) and you're more likely to get people to help you... the volunteers here will help you if you've made the effort.
Sure, I wanted to have some ideas to start at first, but nvm.
$$anonymous$$y code now looks like this: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Game : $$anonymous$$onoBehaviour
{
public Button a, b, c, d;
public Text question;
private Text textA, textB, textC, textD;
void Start()
{
a.onClick.AddListener(() => {
OnButtonClick('a');
});
b.onClick.AddListener(() => {
OnButtonClick('b');
});
c.onClick.AddListener(() => {
OnButtonClick('c');
});
d.onClick.AddListener(() => {
OnButtonClick('d');
});
}
void OnButtonClick(char x)
{
switch(x)
{
case 'a':
Debug.Log("a");
textA = a.GetComponentInChildren<Text>();
Debug.Log(textA.text);
textA.text = "Test";
break;
case 'b':
break;
case 'c':
break;
case 'd':
break;
}
}
}
And it kinda works, but isn't there any better way? Am I able to reference the buttons directly rather than using Unity Editor for it? I'll move the Text GetComponents to Start and will replace the four Buttons with an Array for Buttons, same with Texts, but it's still strange somehow.
//e: Nvm, fixxed it with:
foreach (Button i in answers)
{
Button tmpB = i.GetComponent<Button>();
tmpB.onClick.AddListener(() => {
OnButtonClick(tmpB);
});
}
Your answer
Follow this Question
Related Questions
Button make object teleport (simple) 1 Answer
How to make an object keep move when pressing a button? 0 Answers
Buttons to Scriptable objects 2 Answers
About mobile FPS Sprint 1 Answer