How To Make A 'Answer The Question' Game In 3 Steps - For Beginners

Mr. Unity Buddy's photo
Mr. Unity Buddy
·Mar 21, 2021·

3 min read

How To Make A  'Answer The Question' Game In 3 Steps - For Beginners

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Hi buddies! Today let's make a simple 'Answer The Question' game with Unity. If you are absolutely new to unity, go with our How To Make A Simple Game With Unity tutorial.

Joke Of The Day

R6b2d5980bce5a2d7024e95afabf21988.png

Set Up Your Scene

  • Make a 2D project(2D Template). May be you are new to 2D. But if you read our first tutorial, that won't confuse you ;)
  • First make the the Question you want. Hierarchy > UI > Text
  • Type your question and select a font. To import a new font, Project > Import New Asset.
  • Next make a Input Field. Hierarchy > UI > Input Field.

image.png

Writing Scripts

  • Make a prefab as 'GameManager' and make The 'GameManager' script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    bool gamehasEnded = false;

    public GameObject WinPanel, LosePanel; // Panel that should be activated when Win or Lose

    public void Won()
    {
        WinPanel.SetActive(true); //If the answer is correct, activate WinPanel
    }

   public void Lose()
    {
        if (gamehasEnded == true)

        {
            gamehasEnded = true;
            LosePanel.SetActive(true); //If the answer is incorrect, activate LosePanel
        }
    }
  • Drag and drop the script to the Input Field. Before that, make 2 panels as 'Win Panel' and 'Lose panel'. (Hierarchy > UI > Panel)

  • Remember to add them in the slots in Input Field's inspector.

  • Create a script 'correct_answer'. Then,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class correct_answer : MonoBehaviour
{
    public InputField MainInputField;

    public float Answer = 27; // So we can change it in Inspector.

   public void correctanswer()

    {

        if (MainInputField.text == "" + Answer )
        {
            FindObjectOfType<GameManager>().Won(); //Do the Won() function in GameManager
        }
        else
        {
            FindObjectOfType<GameManager>().Lose(); //Do the Lose() function in GameManager
        }
    }
}
  • Add your script to the InputField.
  • Set On End Edit() function of Input Field to 'correctanswer()'

image.png Now you've successfully created the basic game !

Adding Buttons To The Panels

Now let's add some buttons to our panels.

Win Panel Buttons

  • Make two buttons in your Win Panel. (UI > Button)
  • Name them as 'Next Question' and 'Replay' image.png
  • Make Empty Object as 'Buttons'. And add a script 'Buttons'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Buttons : MonoBehaviour
{
   public void NextLevel()

    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //Load the scene which has scene index +1 than the current scene
    }
   public void Replay()
    {

         SceneManager.LoadScene(SceneManager.GetActiveScene().name);

    }
}
  • Set OnClick() functions.

image.png

Lose Panel Buttons

  • Make a button in Lose Panel as 'Retry'
  • Add same function, 'Replay()'
  • Make Buttons object as a prefab by drag and drop it into the project window
  • Add some levels and drag and drop them to 'Scenes In Build'. (File > Build Settings)

Adding Menus

  • Make two scenes. Start Menu and Game End Menu.
  • In Start Menu, add two buttons as 'Start' and 'Quit'.

image.png

  • In Buttons script,
 public void StartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //Load next scene
    }
    public void Quit()
    {
        Application.Quit();
    }
  • Set Functions. In Game End Menu, create two buttons called 'Restart Game' and 'Quit'.

image.png

  • In Buttons script,
 public void RestartGame()
    {
        SceneManager.LoadScene(0); //Loads Start Menu
    }
  • Now you have successfully finished your project! Well done! Stay with Mr. Unity Buddy for more tutorials!
 
Share this