Trivia game help

Hi guys, so Im trying to create probably one of the most basic games in unity, yet I’m having trouble with it (Im not really a coder, Im learning slowly though). All I want is some basic scripts which allow me to add text to ask a question with 4 choices of an answer and let me select which one is right. Thats it, I just want to write down a question for each question, 4 possible answers, the right one gets you a point (so yeh, a scoring system as well would be fantastic). If anyone could help me with this it would be fantastic. I dont need any interface stuff, just a little bit of code.

Thanks so much

Here’s a great scripting reference page for this problem. It allows you to use Unity’s built in GUI system to create buttons.

http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html

And here’s reference on how to write text to the screen using the same GUI interface.

http://docs.unity3d.com/Documentation/ScriptReference/GUI.Label.html

Hope this helps!

I figured I’d be nice and compile into a script for you. I see you’re very active on the forums so you must be an artist (and we all know how difficult attempting to cross the divide from art <—> programming can be!)

using UnityEngine;
using System.Collections;

public class GUIScript : MonoBehaviour {

	void OnGUI() 
	{
		GUI.Label( new Rect( 10, 10, 200, 50 ), "Question 1: How do you make Unity into a trivia game?" );

		if( GUI.Button( new Rect( 10, 70, 100, 30 ), "Ask AnimalMother." ) )
		{
			print("Correct!");
		}
		
		if( GUI.Button( new Rect( 10, 130, 100, 30 ), "Give up." ) )
		{
			print("Wrong!");
		}
		
	}
}

Thanks so much mate, yeh Im focussing at the moment on stylised art, since Im bad at character modelling and its my best way to make normally bad art look good. If you ever need any help with a model, drop me a PM and I’ll see what I can do.
I’ll test this code out now, honestly I cant tell you how helpful this is.

Although, quick question, its rather embarrassing seeing as you’ve done all this, but what do I apply it to. I’ve put it all in a C# script, but dont know what to do with it.

This is also really helpful, so thanks for that as well mate!

As I said before, if you need anything drop me a PM, as this is a life saver. Thanks!

It’s no problem, and I appreciate the offer!

Do you mean as far as what to do with the script? If that’s what you mean, just create an empty game object by going to GameObject->Empty GameObject (or pressing control+shift+n). Then drag the script from your Project view onto that empty gameObject in your Hierarchy view, run the game and the example GUI I gave you should pop-up.

Do you know how to create functions or use variables? If you don’t, you may want to have a look into that as well, as what I’ve given you will only display a single question and display whether or not the player answered correctly into the Console window (should be the bottom portion of your screen, tab is called “Console”). It’s used for testing programs and printing test information for programmers, not for a finished game. You’re going to want to use a GUI label or display an image to the screen when the player selects an answer, of course.

If you need any help with doing multiple questions I could throw the code for a quick demo up, it may give you an infrastructure to do a substantial portion of your project. Just let me know.

Thanks so much, some example code would be great, I always find it easier to learn from practical examples and at the moment Im a little stumped.

Also, when I try to drag the script the empty, I get an error, telling me
‘cant add script
Cant add script behavior QAScript (thats what I named the script). The scripts file name does not match the name of the class defined in the script.’

As I said, thank you so much for all your help.

You either need to change the name of your script file (QAScript) or the name of the class in the script (I called it GUIScript), as these two names must match for Unity to recognize it.

Here is the better quiz script, includes a timer for displaying the answer (Correct or Wrong) and gives you an example on how to possibly do multiple questions. From a programmer’s standpoint, and if you wanted to do an entire quiz game, this method is pretty poor from a design standpoint but it’s what I could do in 15 minutes.

using UnityEngine;
using System.Collections;

public class GUIScript : MonoBehaviour {

	private int questionNum = 1;
	private bool displayAnswer = false;
	private string currentAnswer;
	private float timerLength = 1.0f;
	private float timer;

	void Start()
	{
		timer = timerLength;
	}

	void OnGUI() 
	{
		if( displayAnswer )
		{
			GUI.Label( new Rect( 10, 250, 200, 50 ), currentAnswer );
			timer -= Time.deltaTime;
			if( timer <= 0 )
			{
				timer = timerLength;
				displayAnswer = false;
			}
		}

		if( questionNum == 1 )
		{
			GUI.Label( new Rect( 10, 10, 200, 50 ), "Question 1: How do you make Unity into a trivia game?" );
			
			if( GUI.Button( new Rect( 10, 70, 100, 30 ), "Ask AnimalMother." ) )
			{
				questionNum++; // Only increment when correct to go to next question
				displayAnswer = true;
				currentAnswer = "Correct!";
			}
			
			if( GUI.Button( new Rect( 10, 130, 100, 30 ), "Give up." ) )
			{
				displayAnswer = true;
				currentAnswer = "Wrong!";
			}
		}

		if( questionNum == 2 )
		{
			GUI.Label( new Rect( 10, 10, 200, 50 ), "Question 2: How do you make my trivia game have multiple questions?" );
			
			if( GUI.Button( new Rect( 10, 70, 100, 30 ), "Blah" ) )
			{
				questionNum++;
				displayAnswer = true;
				currentAnswer = "Correct!";
			}
			
			if( GUI.Button( new Rect( 10, 130, 100, 30 ), "BlHHH" ) )
			{
				displayAnswer = true;
				currentAnswer = "Wrong!";
			}
		}
	}
}

Thanks so much, this script is working perfectly, I’ll need to adjust the buttons a bit but you’ve done too much and the rest for me already, so thanks so much for all your help. Its actually helped me learn coding a lot as well, you know when you open up an electronic device and you see what makes what happen, its like that, I can where your getting all the text to display and so on.
Its gonna be a long night, since my quiz has over 1000 questions lol, but thanks so much for your help!

Holy crap, 1000 questions? Is this for work? Why are they giving a programmer’s job to an artist, seems strange.

But if I were you that sounds like a job for an external file reader. I won’t write one for you, but if you know what you’re doing they usually only take roughly an hour (maybe around 4 if you don’t) and you can just pull in the information from a text file. Might want to look into that. I’ll put a forum post here that, apparently, answers the question in case you’re interested.

http://forum.unity3d.com/threads/2279-Read-text-file-line-by-line-in-C

You can just use that code to pull in your questions and answers (maybe delimiting each answer by colons and new lines to delimit new questions) and place them in an array so you can later loop through them. That way you can just hand write your questions into a text file and have Unity display them all with roughly the same amount of code I’ve already handed you (maybe even less!).

Oh no its for a little app project Im making for my grandad, who made a very old sports trvivia board game back in the day and wants me to help him revive it. Thanks for the link, I’ll look into that, and if it doesnt work out as I said what you provided should work just fine if I put the graft in.

Thanks again