- Home /
Levels Loading Randomly
I made a Breakout clone game after following a tutorial on YouTube. The game works with the way I have levels 1 and 2 set up, but when I add more levels they load randomly. I have them loaded in the Build Settings and I have them being pulled from the code using Application.LoadLevel but they keep loading randomly.
Here is my code from the BrickScript and a screenshot of the Build Settings. Not really sure what's going on to make the levels load randomly since it's not set up that way - at least I don't think they are. (And yes, I did try to contact the tutorial creator but I didn't receive a response.)
Screenshot: http://flic.kr/p/ejzbwE
Code: using UnityEngine; using System.Collections;
public class BrickScript : MonoBehaviour {
static int numBricks = 0;
public int pointValue = 1;
public int hitPoints = 1;
// Use this for initialization
void Start () {
numBricks++;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col) {
hitPoints--;
if(hitPoints <= 0){
Die();
}
}
void Die(){
Destroy(gameObject);
PaddleScript paddleScript = GameObject.Find("Paddle").GetComponent<PaddleScript>();
paddleScript.AddPoint(pointValue);
numBricks--;
Debug.Log (numBricks);
if(numBricks <= 0){
//Load a new level
Application.LoadLevel("level2");
Application.LoadLevel("level3");
Application.LoadLevel("level4");
Application.LoadLevel("level5");
Application.LoadLevel("youWin");
}
}
}
You are trying to load every single level at the same time..
Answer by randomuser · May 15, 2013 at 11:31 PM
I think I know what is wrong.
What you want to do is load the next level. However, you are adding all the next level code to the same place
if(numBricks <= 0){
//Load a new level
Application.LoadLevel("level2");
Application.LoadLevel("level3");
Application.LoadLevel("level4");
Application.LoadLevel("level5");
Application.LoadLevel("youWin");
}
This code here loads every level at once ... so the level that appears could be any one of them. To fix this add a variable that checks to see what level this is and load the consecutive level. If you have any questions on how to do this ask and I will give a example.
EDIT
How to fix the code:
Okay. The essayist option would be to create a public integer that you can edit in the unity editor.
You would set it to the number of the level in the editor and then come back to it in the code
public int level;
//Now you load the level
if(numBricks <= 0){
if(level == 1) {
Application.LoadLevel("level2");
}
else if(level == 2) {
Application.LoadLevel("level3");
}
else if(level == 3) {
Application.LoadLevel("level4");
}
else if(level == 4) {
Application.LoadLevel("level5");
}
else {
Application.LoadLevel("youWin");
}
}
Actually I will ask, how do you do a check for the new level? I'm guessing it's going to be an if statement but I want to make sure I set it up correctly. If statements and I don't usually get along well. -__-
Ok so I tried the code you gave me, and it worked 1 time then got weird. I was able to load the second level with no trouble the first time but then it went straight to the "You Win!" scene after the 2nd level was complete. I tried formatting the code in $$anonymous$$onoDevelop and when I tried again it went straight to the "You Win!" scene after the 1st level was complete. Here's the new code. $$anonymous$$aybe I did something odd? (It has been a long work day for me - I should probably stop trying to code for the night. :P)
public class BrickScript : $$anonymous$$onoBehaviour {
static int numBricks = 0;
public int pointValue = 1;
public int hitPoints = 1;
public int level;
// Use this for initialization
void Start () {
numBricks++;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col) {
hitPoints--;
if(hitPoints <= 0){
Die();
}
}
void Die(){
Destroy(gameObject);
PaddleScript paddleScript = GameObject.Find("Paddle").GetComponent<PaddleScript>();
paddleScript.AddPoint(pointValue);
numBricks--;
Debug.Log (numBricks);
if(numBricks <= 0)
{
if(level == 1)
{
Application.LoadLevel("level2");
}
else if(level == 2)
{
Application.LoadLevel("level3");
}
else if(level == 3)
{
Application.LoadLevel("level4");
}
else if(level == 4)
{
Application.LoadLevel("level5");
}
else
{
Application.LoadLevel("youWin");
}
}
}
}
Did you change that value of level of level in every scene to be the correct one?
Edit Oh sweet christ is works! Thank you soo much! This has been driving me crazy for days trying to get this to work! $$anonymous$$arking your answer as the correct one and going to bed for some very much needed sleep. Thanks again! ^_^
Edit Never$$anonymous$$d I found it! Didn't have the area open. I'll give it a try now.
You know what, I didn't. I'm trying to find that now in the editor. When you select the scene, will it show up there? Sorry if this is pretty sad sounding - I'm still new at this.