Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mih4wK · Dec 22, 2015 at 09:10 AM · buttonbuttonsquiz

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Dec 21, 2015 at 10:52 PM 0
Share

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.

avatar image Mih4wK · Dec 22, 2015 at 08:42 AM 0
Share

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);
             });
         }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Button Flickering On and Off When Pressed 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges