Hello i have been working on making a quiz game in unity that consists of True or False , at the moment i am using text to display whether the answer is wrong or correct, but i would like to use images instead, but i have no idea how to go about it . Any help is Appreciated . Here is my game manger code . Thanks in advance.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public Question[] questions;
private static List<Question> unansweredQuestions;
private Question currentQuestion;
[SerializeField]
private Text factText;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
[SerializeField]
private float TimeBetweenQuestions = 1f;
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
SetCurrentQuestion();
}
void SetCurrentQuestion ()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
factText.text = currentQuestion.fact;
if (currentQuestion.isTrue)
{
trueAnswerText.text = "Like A Boss!";
falseAnswerText.text = "False";
}else
{
trueAnswerText.text = "WRONG";
falseAnswerText.text = "Like A Boss!";
}
}
IEnumerator TransitionToNextQuestion ()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(TimeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void UserSelectTrue ()
{
animator.SetTrigger("True");
if (currentQuestion.isTrue)
{
Debug.Log("LIKE A BOSS!");
} else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFalse()
{
animator.SetTrigger("False");
if (!currentQuestion.isTrue)
{
Debug.Log("LIKE A BOSS!");
}
else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
}
}
1 Like
A good way to do this is simply place the wrong and correct image in the scene. Turn them both off. When you get a wrong answer, turn wrong image on. Then either do an invoke call to turn it off after a few seconds or turn it off when you display the next question. Do the same for the correct image.
@Kharief I know you pmed me, but I’ll post a little more help here. Really, you probably already know how to do this, it should be easy if you give it some thought.
Basically though, add two images to your scene. You can use the Unity UI image for this. Set the sprites to a wrong and a right. Then turn off the gameobjects. Then in your script, add two public Gameobject variables. Tie these in in the inspector.
Now when a person takes a guess, if they get the right answer, you turn on the right image. Then, when you go to display your next question, turn off the images.
So just a few lines of code
public GameObject rightImage;
public GameObject wrongImage;
if(correctAnswer)
rightImage.SetActive(true);
//Or set the wrongImage active if wrong answer.
This should get you started. If you get stuck while trying things out, feel free to ask for more help, but at least try it out before hand.
Here is my updated gamemanger script with @Brathnann code. i followed his instructions. First i created 2 ui images renamed each of them right and wrong , then I selected their source image, turned them off, then I copied his code into my gamemanger script and I keep getting a bunch of errors such as (A namespace can only contain types and namespace declarations (98,19).
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public Question[] questions;
private static List<Question> unansweredQuestions;
private Question currentQuestion;
[SerializeField]
private Text factText;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
[SerializeField]
private float TimeBetweenQuestions = 1f;
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
SetCurrentQuestion();
}
void SetCurrentQuestion ()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
factText.text = currentQuestion.fact;
if (currentQuestion.isTrue)
{
trueAnswerText.text = "Like A Boss!";
falseAnswerText.text = "False";
}else
{
trueAnswerText.text = "WRONG";
falseAnswerText.text = "Like A Boss!";
}
}
IEnumerator TransitionToNextQuestion ()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(TimeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void UserSelectTrue ()
{
animator.SetTrigger("True");
if (currentQuestion.isTrue)
{
Debug.Log("LIKE A BOSS!");
} else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFalse()
{
animator.SetTrigger("False");
if (!currentQuestion.isTrue)
{
Debug.Log("LIKE A BOSS!");
}
else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
}
}
public GameObject rightImage;
public GameObject wrongImage;
if(correctAnswer)
rightImage.SetActive(true);
//Or set the wrongImage active if wrong answer.
Well, you didn’t put the code within the class, so it isn’t going to work. The two public GameObjects need to be with your other variables within the class. (basically with your public Question[ ] questions) part. Then you’ll want to drag the image objects into the variables.
The if statement I gave you was an example. You should put the rightImage.SetActive(true) part with your code that checks if the answer is correct. That way it turns on that image. The wrongImage.SetActive(true) would go with the code that checks for the wrong answer.
Thank you for your help if had progressed me but there is an issue, when i’m tieing the sprites to the script in the inspector they dont show up. i named then right and wrong but it doesnt apear. Please help
You have ui images in your scene. The sprites go into the source image part of those image components. Then you uncheck the gameobject box (so they are inactive).
On your code, you should drag and drop the gameobjects onto the variables in the inspector. Then to turn them on, you use the rightImage.SetActive(true).
i did exactly that but it still doesnt work. i couldnt drag it in. It only scans prefabs so i dragged the imaes into the script made a prefab tied it but it still does not work
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public Question[] questions;
private static List<Question> unansweredQuestions;
public GameObject rightImage;
public GameObject wrongImage;
private Question currentQuestion;
[SerializeField]
private Text factText;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
[SerializeField]
private float TimeBetweenQuestions = 1f;
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
SetCurrentQuestion();
}
void SetCurrentQuestion ()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
factText.text = currentQuestion.fact;
if (currentQuestion.isTrue)
{
trueAnswerText.text = "Like A Boss!";
falseAnswerText.text = "False";
}else
{
trueAnswerText.text = "WRONG";
falseAnswerText.text = "Like A Boss!";
}
}
IEnumerator TransitionToNextQuestion ()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(TimeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void UserSelectTrue ()
{
animator.SetTrigger("True");
if (currentQuestion.isTrue)
{
Debug.Log("LIKE A BOSS!");
} else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
if (currentQuestion.isTrue)
rightImage.SetActive(true);
}
public void UserSelectFalse()
{
animator.SetTrigger("False");
if (!currentQuestion.isTrue)
{
Debug.Log("LIKE A BOSS!");
}
else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
if (currentQuestion.isTrue)
wrongImage.SetActive(true);
}
}
It doesn’t just do prefabs. If the gameobjects are in your scene, you can add them to the variables in the inspector. Are you turning off the gameobject or disabling the image component?
1 Like
i’m sorry it doesnt work i am going to make a video showing everything i’m doing
I made a video about my problem here :
It doesn’t pick them up because you aren’t adding them to the script in your scene. You have to put your gamemanager script in your scene and add the images to the variable slots in the scene.
I see you have a gamemanager object in the scene, so I’m going to guess you have the script on it. Click on it, and drag the images into their slots.
1 Like
Thank you its works well and i am grateful for all your help but there is one problem , watch this video
The image stays on one side instead on moving to the other side.
Well…of course they don’t change sides. Probably your best bet is just to set the image position to whatever side you want. If you want it on top of the answer given, you need to just set it’s position there. (or localposition, depending on what works).
So, set the images position, then turn it on, that way it is on whatever side you want it to be on.