Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by ytwithlove · May 15, 2013 at 10:07 PM · levelsrandomly

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");
             
         }
     }
 }
Comment
Add comment · Show 1
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 ByteSheep · May 15, 2013 at 10:39 PM 1
Share

You are trying to load every single level at the same time..

1 Reply

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

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");
 }
          
                }
Comment
Add comment · Show 6 · 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 ytwithlove · May 16, 2013 at 12:30 AM 0
Share

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. -__-

avatar image randomuser · May 16, 2013 at 12:38 AM 0
Share

Here you go, this should fix it

avatar image ytwithlove · May 16, 2013 at 01:15 AM 0
Share

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");
             }
         }
     }
 }
avatar image randomuser · May 16, 2013 at 01:17 AM 0
Share

Did you change that value of level of level in every scene to be the correct one?

avatar image ytwithlove · May 16, 2013 at 01:24 AM 0
Share

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.

Show more comments

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Switch level script problem 0 Answers

How to make randomly generated levels from a set of prefabs? 0 Answers

The right way to generate levels like Knife Hit or Paint hit game style. 1 Answer

level loader help m plzzzzz 1 Answer

One player, Many levels 4 Answers


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