How to change scenes based on an if else statement C#
I'm making a rhythm game using a list, and when the tracker hits the location of, say, 3 it spawns a note on the third string. I would like to make it so that when noteMark == 72, the scene changes to (2). Unfortunately, I'm a beginner so I don't know how to make this work (have tried many times with if else statements, and javascript but i couldnt link it to the c# variable). Could somebody help me out? I'll put in the code for reference. When you answer, please also advise me WHERE to put it in the code.
public class gm : MonoBehaviour
{
List<float> whichNote = new List <float>() { 1, 6, 3, 4, 2, 5, 2, 1, 2, 3, 5, 6, 4, 6, 5, 5, 1, 2, 4, 1, 1, 4, 5, 1, 3, 2, 5, 1, 6, 4, 5, 3, 1, 2, 2, 5, 6, 2, 4, 5, 4, 1, 6, 2, 4, 3, 5, 1, 5, 4, 3, 6, 3, 2, 4, 5, 6, 4, 1, 1, 6, 2, 3, 6, 1, 6, 2, 5, 4, 1, 3, 2, 2,};
//creates a list of values which can be called based on their position
public int noteMark = 0;
//tracks position of note, starting at 0
public Transform noteObj;
//variable storing the prefab of the note
public string timerReset = "y";
public float xPos;
//the X position of where the note will be spawned (decides which of 5 rows it'll go down)
public static int winStreak = 0;
//variable for how many consecutive notes you get right
public static float totalScore = 0;
//variable for score
public Transform fountainfw;
//variable storing the prefab of the fireworks
public string fountainSpawnL = "n";
//variable checking whether the left fountain has been spawned
public string fountainSpawnR = "n";
//variable checking whether the right fountain has been spawned
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if ((timerReset == "y") && (noteMark<72))
{
StartCoroutine(spawnNote());
//runs the IEnumerator code
timerReset = "n";
//so the notes don't spawn on top of each other every frame, timer is reset to give it enough time
}
if ((winStreak == 5) && (fountainSpawnL == "n"))
{
fountainSpawnL = "y";
Instantiate(fountainfw, new Vector3(2.35f, 1.32f, 0.59f), fountainfw.rotation);
//when winstreak is 5, first sparkle fountains comes up
}
if ((winStreak == 10) && (fountainSpawnR == "n"))
{
fountainSpawnR = "y";
Instantiate(fountainfw, new Vector3(-2.33f, 1.32f, 0.59f), fountainfw.rotation);
//when winstreak is 5, second sparkle fountain comes up
}
}
IEnumerator spawnNote()
{
yield return new WaitForSeconds (0.7f);
//it waits 0.65 seconds, then spawns the note
if (whichNote [noteMark] == 1) {
xPos = -1.35f;
}
//When the note reads value 1 on the list it spawns a note on the last string
if (whichNote [noteMark] == 2) {
xPos = -0.8f;
}
//When the note reads value 2 on the list it spawns a note on the second last string (and so on)
if (whichNote [noteMark] == 3) {
xPos = -0.3f;
}
if (whichNote [noteMark] == 4) {
xPos = 0.35f;
}
if (whichNote [noteMark] == 5) {
xPos = 0.9f;
}
if (whichNote [noteMark] == 6) {
xPos = 1.3f;
}
noteMark += 1;
//Moves the position of the next note
timerReset = "y";
Instantiate (noteObj, new Vector3 (xPos, 2.15f, -3), noteObj.rotation);
//adds these when the program starts; the note object, the top of guitar, an the rotation of the note object
}
}
Answer by CurtMC · Oct 23, 2017 at 08:29 AM
if (noteMark == 72)
{
SceneManager.LoadScene(2);
}
Just add this someplace you want.
Your answer
Follow this Question
Related Questions
Start function not called after reloading scene 0 Answers
How I can Open a Previous Scene with button back !? 3 Answers
Trouble set the scene active with SceneMananger 1 Answer
Loading and Unloading scenes 2 Answers
Unable to get name of current scene 0 Answers