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

Correct answer to my question?

Discussion in 'Editor & General Support' started by RKM_91, Apr 2, 2015.

  1. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Hi,

    I'm trying to script or find out a way i can make my answer buttons do something.

    So the question I have is "What year did Ebola begin?"
    Answer is : 1976

    At the moment the buttons don't do anything so how do I assigned a correct answer to button 1976?

    What I would like to happen is if the player clicks 1976, the window will close and 1 point will be added to the score.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Question1 : MonoBehaviour {
    6.  
    7.     private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    8.     public bool question1;
    9.  
    10.  
    11.     void OnGUI()
    12.     {
    13.         windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    14.     }
    15.  
    16.  
    17.     void WindowFunction (int windowID)
    18.     {
    19.         // Draw any Controls inside the window here
    20.         GUI.Label(new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
    21.    
    22.         if (GUI.Button (new Rect (20, 100, 100, 30), "1976")); //Correct answer
    23.         if (GUI.Button (new Rect (280, 100, 100, 30), "1986")); //Wrong answer
    24.         if (GUI.Button (new Rect (20, 150, 100, 30), "1996")); // wrong answer
    25.         if (GUI.Button (new Rect (280, 150, 100, 30), "1966")); // wrong answer
    26.     }
    27.  
    28. }
     
    Last edited: Apr 2, 2015
  2. louisgv

    louisgv

    Joined:
    Aug 7, 2013
    Posts:
    18
    The button itself inside the OnGUI method is already an if statement. Here's what you could do to make that button do something. Imagine you have a private int score at the top:

    Code (CSharp):
    1. if (GUI.Button (new Rect (20, 100, 100, 30), "1976")){//Correct answer
    2.       ++score; // or score +=1;
    3.       Debug.Log("Correct!"); // Or just this line will tell you something happened.
    4. }