Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Randomise the position of answers in buttons for question?

Discussion in 'Scripting' started by RKM_91, Apr 24, 2015.

  1. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Hi Unity Answers,

    Is there away where I can make the answers to the question below appear in different positions randomly because at the moment every time the question appears on screen the answers are in the same position. Can it be incorporated into my code without many changes?

    Someone posted about Lists but I'm not sure how to use them in my code without errors, so could someone show me how to do this please!

    All help, suggestions and ideas welcome. Thanks.




    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.UI;
    6.  
    7. public class Question1 : MonoBehaviour {
    8.    
    9.     private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    10.     public bool question1;
    11.     private int count;
    12.     public Text countText;
    13.     private bool showTimer = true;
    14.     private float Timer = 10f;
    15.     public AudioSource countdownAudio;
    16.     public AudioSource correctanswerAudio;
    17.     public AudioSource wronganswerAudio;
    18.     private List<string> answers = new List<string>{"1976", "1986", "1996", "1996"};
    19.    
    20.    
    21.    
    22.     void start()
    23.     {
    24.         count = 0;
    25.         SetCountText ();
    26.     }
    27.    
    28.    
    29.     void FixedUpdate(){
    30.         if (showTimer == true) {
    31.             Timer -= Time.deltaTime;
    32.         }
    33.        
    34.         if (Timer <= 0f) {
    35.             showTimer = false;
    36.             Timer = 10f;
    37.             Destroy (this.gameObject);
    38.            
    39.         }
    40.     }
    41.    
    42.    
    43.     void OnGUI(){
    44.         {
    45.             windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    46.         }
    47.     }
    48.    
    49.    
    50.     void WindowFunction (int windowID)
    51.     {
    52.        
    53.        
    54.         GUI.Label (new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
    55.        
    56.         if (GUI.Button (new Rect (20, 100, 100, 30), "1976")) // Correct Answer
    57.         {
    58.             AudioSource source = countdownAudio.GetComponent<AudioSource>();
    59.             source.mute = true;
    60.             correctanswerAudio.Play();
    61.             Destroy (this.gameObject);
    62.             count = count += 1;
    63.             SetCountText ();
    64.            
    65.         }
    66.        
    67.         if (GUI.Button (new Rect (280, 100, 100, 30), "1986")) //Wrong answer
    68.         {
    69.             AudioSource source = countdownAudio.GetComponent<AudioSource>();
    70.             source.mute = !source.mute;
    71.             wronganswerAudio.Play();
    72.             Destroy (this.gameObject);
    73.         }
    74.        
    75.         if (GUI.Button (new Rect (20, 150, 100, 30), "1996")) // wrong answer
    76.         {
    77.             AudioSource source = countdownAudio.GetComponent<AudioSource>();
    78.             source.mute = !source.mute;
    79.             wronganswerAudio.Play();
    80.             Destroy (this.gameObject);
    81.         }
    82.        
    83.         if (GUI.Button (new Rect (280, 150, 100, 30), "1966")) // wrong answer
    84.         {
    85.             AudioSource source = countdownAudio.GetComponent<AudioSource>();
    86.             source.mute = !source.mute;
    87.             wronganswerAudio.Play();
    88.             Destroy (this.gameObject);
    89.         }
    90.        
    91.         if (showTimer == true)
    92.         {
    93.             GUI.Label (new Rect (300, 25, 200, 50), "Timer: " + Timer.ToString ("F0"));
    94.            
    95.         }
    96.        
    97.        
    98.        
    99.     }
    100.    
    101.     void SetCountText()
    102.     {
    103.         ValuesHolder.answersCount++;
    104.         countText.text = "Score: " + ValuesHolder.answersCount.ToString();
    105.     }
    106.    
    107.    
    108. }
    109.  
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Put your answers in a list than use Random.Range with the lists length, get the question you want using the result of Random.Range as the index. Than remove the element you just got from the list using RemoveAt method of the list. Rinse and repeat till you got all the answers you want.
     
  3. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Thanks for you reply, not sure how to do what you said, could you please post an example?