- Home /
Switching between several levels
How do i have my character switch between several levels. I used Application.Loadlevel(1); But that keeps bringing me back to the same level everytime. How do i make it go from level 1 to level 2 to level 3 and so on? Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public GUIText countText;
public GUIText winText;
private int count;
void Start ()
{
count = 0;
SetCountText ();
winText.text ="";
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 1)
{
winText.text = "CONGRAGULATIONS!";
Application.LoadLevel(1);
}
}
}
Thank you in advance!
Answer by Dave-Carlile · Jun 18, 2015 at 06:05 PM
A quick look at the Application.LoadLevel documentation shows that the parameter you pass is the level index, or level number. You're always passing 1, so you're always loading level 1.
You need to pass the level number that you want to load. You could keep track of it in a levelNumber
field much like you do the count
field you have now. Increment it by one when the level is complete, pass that variable into the LoadLevel
function.
That's what i was thinking, but im not to sure how to write the code up for it. Do you have an example i can go off of? Would it be: Application.LoadLevel = Application.LoadLevel + 1;?
I'm not going to write your code for you. :)
You already have a count
variable that is incrementing by 1 when the person picks something up. You need to declare a levelNumber
variable the same way count
is declared - you should be able to see how that's done in your code. And you need to increment it by 1 before calling LoadLevel
(again, you have code in your example that increments count
by 1), and ins$$anonymous$$d of passing 1
to LoadLevel
you pass your levelNumber
variable ins$$anonymous$$d.
So i added levelNumber to my code,
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour
{
public float speed;
public GUIText countText;
public GUIText winText;
private int count;
public int levelNumber;
void Start ()
{
count = 0;
levelNumber = 0;
SetCountText ();
winText.text ="";
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
levelNumber = levelNumber + 1;
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 1)
{
winText.text = "CONGRAGULATIONS!";
Application.LoadLevel(levelNumber + 1);
}
}
}
It keeps replaying the same level over and over again. What did i do wrong?
You're close. You don't want to increment levelNumber
when the player picks up something - you would only want to do that right before you're ready to load the next level, inside your if (count >= 1)
. And then you would only pass levelNumber
to LoadLevel
, not levelNumber + 1
. Finally, you probably want to initialize levelNumber
to 1 ins$$anonymous$$d of 0.
Finally, right before you call LoadLevel
, add Debug.Log("Level Number: " + levelNumber)
. $$anonymous$$ake sure that's displaying the correct level number.
Im confused with adding the levelNumber to the if(count>= 1).
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 1)
(levelNumber == levelNumber + 1)
{
winText.text = "CONGRAGULATIONS!";
Debug.Log ("Level Number: " + levelNumber);
Application.LoadLevel(levelNumber);
}
}
}
does this look like a step in the right direction?