Home > Software engineering >  change scene upon reaching specific score
change scene upon reaching specific score

Time:07-18

I'm very new to unity and coding so I have some doubt here. I'm current creating a multiple choice quiz game. can someone help w the coding for my question. My question here is I need to change scene upon reaching certain point. for example, when my score hits 30 I want winner page to appear and when my score hits 0 I want you lose page to appear. how?

CodePudding user response:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class AnswerButtons : MonoBehaviour

{ public GameObject answerAbackBlue; // blue is waiting public GameObject answerAbackGreen; // green is correct public GameObject answerAbackRed; // red is wrong answer

public GameObject answerBbackBlue; // blue is waiting
public GameObject answerBbackGreen; // green is correct 
public GameObject answerBbackRed; // red is wrong answer

public GameObject answerCbackBlue; // blue is waiting
public GameObject answerCbackGreen; // green is correct 
public GameObject answerCbackRed; // red is wrong answer

public GameObject answerDbackBlue; // blue is waiting
public GameObject answerDbackGreen; // green is correct 
public GameObject answerDbackRed; // red is wrong answer

public GameObject answerA;
public GameObject answerB;
public GameObject answerC;
public GameObject answerD;

public AudioSource CorrectFX;
public AudioSource WrongFX;

public GameObject currentScore;
public int scoreValue;

void Update()
{
    currentScore.GetComponent<Text>().text = "SCORE:  "   scoreValue;

  if (scoreValue > 300)
        SceneManager.LoadScene("WinnerPage");

    if (scoreValue < 50)
        SceneManager.LoadScene("LoserPage");

}

 

public void AnswerA()
{
    if (QuestionGenarate.actualAnswer == "A")
    {
        answerAbackGreen.SetActive(true);
        answerAbackBlue.SetActive(false);
        CorrectFX.Play();
        scoreValue  = 10;
    }
    else
    {
        answerAbackRed.SetActive(true);
        answerAbackBlue.SetActive(false);
        WrongFX.Play();
        scoreValue -= 5;
    }
    answerA.GetComponent<Button>().enabled = false;
    answerB.GetComponent<Button>().enabled = false;
    answerC.GetComponent<Button>().enabled = false;
    answerD.GetComponent<Button>().enabled = false;
    StartCoroutine(NextQuestion());
}

public void AnswerB()
{
    if (QuestionGenarate.actualAnswer == "B")
    {
        answerBbackGreen.SetActive(true);
        answerBbackBlue.SetActive(false);
        CorrectFX.Play();
        scoreValue  = 10;
    }
    else
    {
        answerBbackRed.SetActive(true);
        answerBbackBlue.SetActive(false);
        WrongFX.Play();
        scoreValue -= 5;
    }
    answerA.GetComponent<Button>().enabled = false;
    answerB.GetComponent<Button>().enabled = false;
    answerC.GetComponent<Button>().enabled = false;
    answerD.GetComponent<Button>().enabled = false;
    StartCoroutine(NextQuestion());
}

public void AnswerC()
{
    if (QuestionGenarate.actualAnswer == "C")
    {
        answerCbackGreen.SetActive(true);
        answerCbackBlue.SetActive(false);
        CorrectFX.Play();
        scoreValue  = 10;
    }
    else
    {
        answerCbackRed.SetActive(true);
        answerCbackBlue.SetActive(false);
        WrongFX.Play();
        scoreValue -= 5;
    }
    answerA.GetComponent<Button>().enabled = false;
    answerB.GetComponent<Button>().enabled = false;
    answerC.GetComponent<Button>().enabled = false;
    answerD.GetComponent<Button>().enabled = false;
    StartCoroutine(NextQuestion());
}

public void AnswerD()
{
    if (QuestionGenarate.actualAnswer == "D")
    {
        answerDbackGreen.SetActive(true);
        answerDbackBlue.SetActive(false);
        CorrectFX.Play();
        scoreValue  = 10;
    }
    else
    {
        answerDbackRed.SetActive(true);
        answerDbackBlue.SetActive(false);
        WrongFX.Play();
        scoreValue -= 5;
    }
    answerA.GetComponent<Button>().enabled = false;
    answerB.GetComponent<Button>().enabled = false;
    answerC.GetComponent<Button>().enabled = false;
    answerD.GetComponent<Button>().enabled = false;
    StartCoroutine(NextQuestion());
}

IEnumerator NextQuestion()
{
    yield return new WaitForSeconds(2);

    answerAbackGreen.SetActive(false);
    answerBbackGreen.SetActive(false);
    answerCbackGreen.SetActive(false);
    answerDbackGreen.SetActive(false);
    answerAbackRed.SetActive(false);
    answerBbackRed.SetActive(false);
    answerCbackRed.SetActive(false);
    answerDbackRed.SetActive(false);
    answerAbackBlue.SetActive(false);
    answerBbackBlue.SetActive(false);
    answerCbackBlue.SetActive(false);
    answerDbackBlue.SetActive(false);
    answerA.GetComponent<Button>().enabled = true;
    answerB.GetComponent<Button>().enabled = true;
    answerC.GetComponent<Button>().enabled = true;
    answerD.GetComponent<Button>().enabled = true;
    QuestionGenarate.displayingQuestion = false;
}

}

CodePudding user response:

It is hard to answer because you didn't show us the exact code where you count scores. But I will try to help you as much as I can.

Enter your code where you count scores (where you have that variable that stores scores). Now, above the code, among other "using" statements add

```
using UnityEngine.SceneManagement;
```

So you just added a scene manager into your script to have control over scenes. Now inside your Update() function add

```
if (score<0)
SceneManager.LoadScene("LoserPage"); //player lost

if (score>29)
SceneManager.LoadScene("WinnerPage"); //player won

```

In the code above score is a variable that stores your scores. Change that to whatever you have that does it. LoserPage and WinnderPage are names of the scenes that you have created. Change their names to whatever you have to make it work. Don't forget to add your scenes in Build settings in correct order.

Hopefully I was helpful. Good luck.

  • Related