Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 SamPav · Oct 28, 2015 at 05:53 PM · uibuttontextbutton trigger eventsquiz

Problem with highlighting correct answer, any idea?

Hi guys, I am making an educational quiz game. I already have a lot of lines of code so I ll try to simplify my problem as much as I can. Lets say I have 4 UI texts for 4 answers. Each has a button component and a script component attached.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Quiz : MonoBehaviour {
 
 public string Answer;
 public string correctAnswer;
 
 void Start () {
 GetComponent<Button>().onClick.AddListener(() => { Method(Answer); });     // this calls the method after I click on the text (where I have a button component)
 }
 
 public void Method (string passedAnswer) {
 if (passedAnswer == correctAnswer)
 { GetComponent<Text> ().color = Color.green; // if I click on correct answer, the text color changes to green
 }
 else
 {GetComponent<Text>().color = Color.red; 
 // HERE, how do I find a UI text where the answer variable equals correct answer? (so that I could highlight it to green?)
 }
 }
 }

Although it is super easy to highlight the color of text to correct/incorrect color, my problem is this > when I click on incorrect answer how do I find a text where the answer variable equals correctAnswer? (So that I could highlight it to green color or whatever). Thanks a lot in advance!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ElDo · Oct 28, 2015 at 07:58 PM

You could try it like this:

 Quiz[] q=GameObject.FindObjectsOfType<Quiz>();
 Quiz correct=null;
 foreach(Quiz c in q){
  if(c.Answer==correctAnswer)correct=c;
 }

but this code is not tested. The idea behind is to get all Objects and compare to the one that has the correct answer.

Comment
Add comment · Show 1 · Share
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 SamPav · Oct 29, 2015 at 08:55 AM 0
Share

Thanks a lot, that works! :))

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

6 People are following this question.

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

Related Questions

How to disable button text UI 0 Answers

How to do Image Swap with Text Color the with New UI Button Transition? 0 Answers

How to make button click continuous ? 0 Answers

UI Button OnClick Only Works the First Time 2 Answers

How can I generate and setup buttons via code 1 Answer


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