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 TsuRei · Mar 24, 2016 at 01:25 PM · c#guievent

My script for my buttons doesn't seem work? HELP!

Hi, I'm a beginner in programming.

I want to make a quiz game. Have a text (for question) and 2 buttons (options for answer). What I want from my code is, every time I click one of my buttons (right or wrong), the question changes to next question in my list. The score is functioning perfectly, but the question and buttons don't want to change. So, I wonder if there's something wrong with my code?

Here's my code :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 
 public class RandomQuestion : MonoBehaviour {
     
     // Use this for initialization
 
     private List<string[]> questions = new List<string[]>();
     private List<int> answerOrder = new List<int>(new int[] { 1, 2 });
 
     UnityEngine.UI.Text Question;
     public Button aButton1;
     public Button aButton2;
 
     public Text test;
 
 
     void DrawInfo()
     {
         Rect rect = new Rect(500, 100, 400, 200);
         Question = GetComponent<UnityEngine.UI.Text>();
         Question.text = questions[0][0];
         aButton1.gameObject.GetComponentInChildren<Text>().text = questions[0][answerOrder[0]];
         aButton2.gameObject.GetComponentInChildren<Text>().text = questions[0][answerOrder[1]];
         
         if (aButton1.IsActive())
         {
             aButton1.onClick.AddListener(delegate { CheckAnswer(answerOrder[0]); });
         }
         if(aButton2.IsActive())
         {
             aButton2.onClick.AddListener(delegate { CheckAnswer(answerOrder[1]); });
         }
     }
 
     private void CheckAnswer(int answer)
     {
         if (answer == 1)
         {
             //handle correct answer
             GameControl.control.score += 10;
         }
         else {
             //handle wrong answer
             GameControl.control.score -= 10;
         }
         NextQuestion();
     }
 
     void OnGUI()
     {
         test.text = questions.Count.ToString();
         //this is just to protect from an empty list... you will need better win/lose conditions.
         if (questions.Count > 0)
         {
             DrawInfo();
         }
         else
         {
             GameControl.control.Tutorial();
         }
     }
 
     void Start()
     {
         // String order: question, correct, wrong
         questions.Add(new string[] { "What in ?", "aa", "bb"  });
         questions.Add(new string[] { "Which of ?", "cc", "dd"  });
         questions.Add(new string[] { "Which of these ?", "ee", "ff"  });
         Shuffle(questions);
         Shuffle(answerOrder);
         GameControl.control.AddTutorials();
     }
 
     void NextQuestion()
     {
         questions.RemoveAt(0);
         Shuffle(answerOrder);
     }
     
     static readonly System.Random rng = new System.Random();
     public static void Shuffle<T>(IList<T> list)
     {
         int n = list.Count;
         while (n > 1)
         {
             n--;
             int k = rng.Next(n + 1);
             T value = list[k];
             list[k] = list[n];
             list[n] = value;
         }
     }
 }

Sorry for my bad English and thank you.

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

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

133 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create an event trigger script for intercepted events 0 Answers

Loading Game Crash 1 Answer

Multiplayer Hiding Layer just for Local Player 0 Answers

How to change GUI label size 1 Answer

Connect C# script with GuiText 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