- Home /
Go to next scene when correct answer
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.
Answer by FeedMyKids1 · Aug 23, 2020 at 10:59 PM
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.
Thank you so much!! I already make sure all the scenes where added, created the script with the UnityEngine... and pasted the script, but I don't know how can I reference it?? like how can make the script work and change the scene when the click the correct answer button??
You could make it a Singleton and reference it from anywhere or you could put it directly on your Game$$anonymous$$anager.
I would have three components - QuestionAnswerUI, Game$$anonymous$$anager, and SceneLoader:
Your Game$$anonymous$$anager tells your questionAnswerUI.ShowQuestion (Question q) [supposing that also passes the answers] and when the player clicks a button, QuestionAnswerUI calls Game$$anonymous$$anager.ClickedButton (int buttonNum) and you check if answer[buttonNum] is correct. If it is you do all your reward logic and calls back to QuestionAnswerUI.EnableNextSceneButton().
You click that and you have something like
class QuestionAnswerUI
{
private void Start ()
{
nextScene.onClick.AddListener (delegate{ SceneLoader.Instance.TryLoadNext() ;});
int i = 0;
foreach(Button b in answerButtons)
{
int index = i; //$$anonymous$$ust create new int so as not to pass pointer to i
b.onClick.AddListener (delegate { Game$$anonymous$$anager.ClickedButton(index) ;});
i++;
}
}
public void ShowQuestion (Question q)
{
//Post question text
//Load answer texts into buttons and enable them
}
public void EnableNextSceneButton() => nextScene.enabled = true;
}
Again thank you so much!! you really do help, but I messed up and I don't know how but now I can't even add the scripts to the Game manager :( I know this is a lot to ask but is there any way I can send you my project and you can check it out and tell me the things I did wrong and help me, please? If you can't I will completly understand it and (I'm not really sure how this works) but I gave you a point for helping me so much...again thanks!!
Answer by hkl788954 · Aug 27, 2020 at 05:25 AM
Ford Feedback Survey – If you are searching for genuine details on Ford Customer Satisfaction Survey to share your loyal feedback about Ford Survey.
Answer by Aviryx · Aug 23, 2020 at 11:48 PM
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;
}
}
}
Thank you so much! And where can I put this script? on a Game$$anonymous$$anager object or somewhere else? and then I have to put sometyhing on the buttons? Sorry if this are stupid questions, but again I am a begginer :(
And I tried the script and Unity says Error "The type or name space "IEnumerator" could not be found, can you please help me, thank you in advance
For IEnumerator, be sure to have "using System.Collections; "