Hi, so I am new on using unity and I am trying to do a quiz game and I have made 6 scenes and on each of them there is a question and four buttoms and one is the correct answer, I am not sure on how to make it change scene when I click the correct answer or if I can make something say “correct” and go to the next scene or on the opposite if they click the incorrect buttom that it says “try again” and then they have to try again, can someone please help me?
I know this is a stupid question but as I said before I am a very confused begginer so if someone helps me I will thank them for life.
Go to File/Build Settings and make sure to add all the scenes in your project.
Create a script called SceneLoader and put using UnityEngine.SceneManagement;
public class SceneLoader: MonoBehaviour
{
//Call something like this when someone gets the answer right
public void TryLoadNextScene()
{
int currentScene = SceneManager.GetActiveScene().buildIndex;
if (currentScene < SceneManager.sceneCount - 1)
SceneManager.LoadScene(currentScene + 1);
}
}
Make sure to attach the script to something in the scene and reference it so that you can call TryLoadNextScene when you the answer is correctly answered.
You could just replicate this for each scene if you want something basic.
_
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SceneOneManager : MonoBehaviour
{
public GameObject incorrectAnswerPanel_UI;
public Button answerOne;
public Button answerTwo;
public Button answerThree;
void Start()
{
answerOne.onClick.AddListener(IncorrectAnswer);
answerTwo.onClick.AddListener(IncorrectAnswer);
answerThree.onClick.AddListener(CorrectAnswer);
}
private void IncorrectAnswer()
{
incorrectAnswerPanel_UI.SetActive(true);
}
private void CorrectAnswer()
{
StartCoroutine(LoadNextScene());
}
IEnumerator LoadNextScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(2);
while (!asyncLoad.isDone)
{
yield return null;
}
}
}