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
0
Question by Astro_boi25 · Jun 18, 2015 at 06:03 PM · scene-loadingscene-switchingscene loadscene change

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!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 9 · 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 Astro_boi25 · Jun 18, 2015 at 06:10 PM 0
Share

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

avatar image Dave-Carlile · Jun 18, 2015 at 06:24 PM 1
Share

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.

avatar image Astro_boi25 · Jun 19, 2015 at 03:56 PM 0
Share

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?

avatar image Dave-Carlile · Jun 19, 2015 at 04:05 PM 0
Share

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.

avatar image Astro_boi25 · Jun 19, 2015 at 04:43 PM 0
Share

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?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

"Some objects were not cleaned up when closing the scene," though I don't have anything instantiating with OnDestroy() 0 Answers

SceneManager.LoadScene not working with button 1 Answer

What is happening to scene manager.loadscene??? 2 Answers

Problem in loading death menu scene 1 Answer

Canvas gets invisble after changing scene 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