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 /
  • Help Room /
avatar image
0
Question by TrePlaysGames · Jan 05, 2016 at 08:19 AM · unity 5javascriptscripting problem

Next Level Using numbers

I'm trying to make a really simple script (because I'm still new to scripting) so when my 2D character enters the collider the next level will start. I've been literally working on this one script for 30-40minutes. I just need the script to work and for me to be able to edit the numbers in the editor so they can be used on each level and I don't have to remake the script.

Script here:

function onTriggerEnter2D(other: Collider2D) {

SceneManagement.SceneManager.LoadScene("");

}

Once again, not good at scripting but I'm really trying here, any help would be great.

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 ForeignGod · Jan 05, 2016 at 10:43 AM 0
Share

What exactly is the problem? But the desired name or index number in the build settings in between the ("")

5 Replies

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

Answer by Curb · Jan 05, 2016 at 02:13 PM

This should work and give you a public variable in the editor:

 var levelToLoad : int;
 
 function OnTriggerEnter2D(other : Collider2D){
     SceneManagement.SceneManager.LoadScene(levelToLoad);
 }

or a string instead of an int if you want to use names.

The reason it's not working is also because of the function name not starting with a capital O. To check if something is running at all I always like to do a quick Debug.Log("nameOfFunction") when I try to figure out where the problem lies.

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 TrePlaysGames · Jan 05, 2016 at 02:34 PM 0
Share

Alright I'll try it when I get home, thanks for the help.

avatar image
0

Answer by Verusoft · Jan 06, 2016 at 07:49 AM

Do you mean something like this?

Create empty object with SceneLoader.cs script in first level (let's call this level "scene_A").

 using UnityEngine;
 using UnityEngine.SceneManagement;

 public class SceneLoader: MonoBehaviour {
      public static SceneLoader instance;
     
      void Awake() {
         if(!instance) {
             instance = this;
         }
         else {
             DestroyObject(gameObject);
         }
         DontDestroyOnLoad(gameObject);
      }
     
     int currentLevel = 0; 
 
     static readonly string [] gameLevels = new string[] {
         "scene_A",   // 0
         "scene_B",  //  1
         "scene_C"  //   2
     };
 
     public void LoadNextLevel() {
         int nextLevelNuber = (currentLevel + 1) % gameLevels.Length;
         SceneManager.LoadScene(gameLevels[nextLevelNuber]);
         currentLevel = nextLevelNuber;
     }
 }

Edit gameLevels array according to your needs. Add your scenes into build settings. When you want to start another game level call

 SceneLoader.instance.LoadNextLevel();

in onTriggerEnter2D() methods or anywhere where you need.

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 L_Artista · Jan 06, 2016 at 11:31 AM

Next level can be reached using:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

Integrating in your script would look like this:

 function onTriggerEnter2D(other: Collider2D) {
 
 SceneManagement.SceneManager.LoadScene(SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1));
 
 }



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 spec7or · Jan 06, 2016 at 11:39 PM

@TrePlaysGames:

You can easily do what your asking by ensuring that all scenes are in the build order and by extending your script just a little bit.

 //This is the value you assign in the inspector.
 public int mySceneToLoad;
 
 function onTriggerEnter2D(other: Collider2D)
 {
  SceneManagement.SceneManager.LoadScene(mySceneToLoad);
 }

Hope this helps

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 TrePlaysGames · Jan 06, 2016 at 06:08 AM

Thanks it works like a charm! I'll go study more on this scripting too.

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

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

8 People are following this question.

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

Related Questions

New Hierarchy Son Script 0 Answers

What's wrong with my script? 2 Answers

OnCollisonEnter2D Not Firing after checking collider 1 Answer

Can not get my raycast to detect an object 0 Answers

Adding a Highscore to game 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