- Home /
Loadlevel weirdness
Im currently using a parent to hold all my 'enemies' as it were. I then do a check on the parent for its childcount and if this = 0 then I call a function to load the level. I use a yield in there to delay the loading and then increment the level variable so that I am to reuse this code for all levels. However sometimes it will randomly skip levels. My thinking is that the increment gets run more than once possibly due to the yield. Can anyone spot any potential problem here?
void Update ()
{
//Debug.Log (transform.childCount);
if (transform.childCount == 0) {
StartCoroutine (ChangeLevel ());
}
}
IEnumerator ChangeLevel ()
{
yield return new WaitForSeconds (1.5f);
level++;
Application.LoadLevel (level);
}
Answer by Wasagi · Jun 04, 2011 at 04:27 PM
I've figured out the problem. It seems that loadlevel doesnt happen immediately and my check on the children still occurs so while the level is loading the level variable is incremented before the parent is destroyed.
I have added a isLoadingLevel check to prevent this from happening and now I am unable to reproduce the error. I have posted the modified code below, just in case it might benefit someone in the future.
using UnityEngine;
using System.Collections;
public class Balls : MonoBehaviour {
private static int level = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Debug.Log (level);
if (transform.childCount == 0 && !Application.isLoadingLevel) {
StartCoroutine (ChangeLevel ());
}
}
IEnumerator ChangeLevel ()
{
yield return new WaitForSeconds (1.5f);
level++;
Application.LoadLevel (level);
}
}
Answer by Catlard · Jun 04, 2011 at 02:46 PM
When you load a level, everything is erased. Are you telling unity not to destroy this script, so it knows what level you've been in, and therefore where you're going?
Also, how are you calculating level?
Answer by Wasagi · Jun 04, 2011 at 02:49 PM
Ill post the script in its entirety. I'm using a static variable to hold level (I am under the assumption these are persistent? The script is attached to an empty game object which is a parent for my 'enemies'.
using UnityEngine;
using System.Collections;
public class Balls : MonoBehaviour {
private static int level = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//Debug.Log (transform.childCount);
if (transform.childCount == 0) {
StartCoroutine (ChangeLevel ());
}
}
IEnumerator ChangeLevel ()
{
yield return new WaitForSeconds (1.5f);
level++;
Application.LoadLevel (level);
}
}
Answer by Catlard · Jun 04, 2011 at 02:53 PM
Nope! They're not persistent, as far as I know. See here:
http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html
Try fixing it by using that. If it doesn't work, let me know, and I'll look at the code again.
Im 100% sure that status persist. I have used Debug.Log to check the value of level across multiple scenes. 70% of the time I get normal change level behaviour from level 1-6 in my game; however, sometimes it will skip a level.
Ah, sorry about that, then. I don't know what's wrong with the script...hope you find out!
Your answer
Follow this Question
Related Questions
Spawn menu on Level Load 3 Answers
How to teleport to different scenes? 2 Answers
need help with code 1 Answer
Can't go back to the game scene from main menu for the second time 2 Answers
Loading level after sound plays 1 Answer