Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wasagi · Jun 04, 2011 at 02:29 PM · loadlevel

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);


 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);


 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Catlard · Jun 04, 2011 at 04:51 PM 0
Share

Good work! Looks like the solution

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wasagi · Jun 04, 2011 at 02:50 PM 0
Share

Ill post the full code below. Level is a static variable.

avatar image
0

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);


 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wasagi · Jun 04, 2011 at 03:25 PM 0
Share

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.

avatar image Catlard · Jun 04, 2011 at 03:41 PM 0
Share

Ah, sorry about that, then. I don't know what's wrong with the script...hope you find out!

avatar image Wasagi · Jun 04, 2011 at 04:22 PM 0
Share

Thanks anyways :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges