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 sid4 · Oct 14, 2016 at 07:00 PM · scripting problem

Unity changed loadlevel to sceneload now.I cant find how to load the next scene. i checked reference but not specific on code Help!

I cant find how to load the next scene . i checked reference but not specific on code Help

on certain collisions I tried coding the new scene loading.. I was using loadlevel manager and was working but unity changed this and uses scene manager now

on collision my new scene wont load I have tried using the unity reference code but still not working obviously im doing something wrong but theres so little info on this specific topic since unity just changed it and theres no script answers at all. ive looked for days nothing is out there but the older loadlevel script snippets

plase help the new scene I have is "22"

thankyou

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
Best Answer

Answer by TBruce · Oct 14, 2016 at 07:22 PM

Here are some functions that I regularly use (in C#)

 using UnityEngine.SceneManagement; // required for SceneManager
 
 public void RestartGame()
 {
     SceneManager.LoadScene(0);
 }
 
 public void ReloadCurrentScene()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }
 
 public void LoadPreviouScene()
 {
     if (SceneManager.GetActiveScene().buildIndex > 0)
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
     }
     else
     {
         SceneManager.LoadScene(0);
     }
 }
 
 public void LoadNextScene()
 {
     if (SceneManager.GetActiveScene().buildIndex < (sceneCountInBuildSettings -1)
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     }
     else
     {
         ReloadCurrentScene();
     }
 }

if you need Unity script let me know. Also, very important, all scenes need to be added to the Scenes in Build list in the Build Settings screen for this to work (see image below). See Unity docs here - under Description.

Build Settings


build-settings.png (70.7 kB)
Comment
Add comment · Show 15 · 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 TBruce · Oct 16, 2016 at 05:23 PM 0
Share

@sid4 Did this answer help?

avatar image sid4 · Oct 17, 2016 at 10:22 AM 0
Share

hi sorry for my lateness im checking now if it works

avatar image TBruce sid4 · Oct 17, 2016 at 05:32 PM 0
Share

@sid4 See my modified answer above for more information about Scene$$anonymous$$anager.LoadScene.

Also it is important to note that Scene$$anonymous$$anager.GetActiveScene() does not take any parameters, it returns a Scene$$anonymous$$anagement.Scene structure.

It is very important that all scenes be added to the Scenes in Build list in the Build Settings screen for this to work (see image here). See Unity docs here - under Description.

avatar image sid4 · Oct 17, 2016 at 10:31 AM 0
Share

nope I tried several of your codes but its not working heres one I tried

 using System;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 
 namespace UnityStandardAssets._2D
 {
 public class lo : $$anonymous$$onoBehaviour {
     
 
     //void FixedUpdate() {
         
 void        OnCollisionEnter2D(Collision2D coll) {
             
             if (coll.gameObject.tag == "Player")
             //Destroy (gameObject);
         //    Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetSceneAt(11).name);
 
             {
                 if (Scene$$anonymous$$anager.GetActiveScene().buildIndex < (sceneCountInBuildSettings -1)
                     {
                         Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene(22).buildIndex + 1);
                             }
                     else
                     {
                         ReloadCurrentScene();
                     }
                      }
 
 
                 //Application.LoadLevel("11");
 
         }
     }
 
avatar image sid4 · Oct 17, 2016 at 07:54 PM 0
Share

I got the scene to play and your rite I had to add the scenes to the build manually
but the only code that worked is the link u gave me

but it loaded repetitive tons of the scene at the same time on every collision it froze my pc so that additive code wont do it since I guess additive (by nature'} means too keep adding the scene on every collision I just want to add /change to the next scene on collision

I need a code to just change the scene on a collision not add tons of the scenes just change the scene on collision

I have tried all your codes but none work, heres the latest code im with

 using System;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 
 namespace UnityStandardAssets._2D
 {
 public class lo : $$anonymous$$onoBehaviour {
     
 
     //void FixedUpdate() {
         
 //void    OnCollisionEnter2D(Collision2D coll) {
             
              void OnCollisionEnter2D(Collision2D coll) {
             
             Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
 
              //Scene$$anonymous$$anager.LoadScene("22");
             //Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene(22).buildIndex + 1);
             //Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene(22).name);
             //Scene$$anonymous$$anager.LoadScene(22);
             }
 
                 }
 }        

avatar image sid4 · Oct 17, 2016 at 08:14 PM 0
Share

i just tried all the links single and or additive codes the additive did work but - again it keeps adding hundreds of scenes within the scene and freezes my pc that's not what I want at all

I just want a normal transition to the next scene on collision

avatar image TBruce sid4 · Oct 17, 2016 at 08:30 PM 0
Share

Why are you trying to load the scenes in Additive mode? If I understand you, this is your current code

using System; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;

namespace UnityStandardAssets._2D { public class lo : $$anonymous$$onoBehaviour { void OnCollisionEnter2D(Collision2D coll) { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1); } } } It would seem that the OnCollisionEnter2D() is constantly being triggered. I do not know how your scene is set up but the problem can most likely be remedied by doing something like this

 using System;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 namespace UnityStandardAssets._2D
 {
     public class lo : $$anonymous$$onoBehaviour
     {
         private bool sceneIsLoading = false;
         void OnCollisionEnter2D(Collision2D coll)
         {
             // do not try to load scene if sceneIsLoading is set
             if (!sceneIsLoading)
             {
                 // set here so this block is never entered again
                 sceneIsLoading = true;
                 Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
             }
         }
     }
 }

Now you can also do something like this

 using System;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 namespace UnityStandardAssets._2D
 {
     public class lo : $$anonymous$$onoBehaviour
     {
         private int sceneIsLoading = -1;
         void OnCollisionEnter2D(Collision2D coll)
         {
             // do not try to load scene if sceneIsLoading is set
             if (sceneIsLoading< 0)
             {
                 // set here so this block is never entered again
                 sceneIsLoading = Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1;
                 Scene$$anonymous$$anager.LoadScene(sceneIsLoading);
             }
         }
     }
 }

But remember this

 Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1

is not safe, this is because you will get an error if Scene$$anonymous$$anager.GetActiveScene().buildIndex is the last scene.

That is where my original script comes in. You create a class (call it Game$$anonymous$$anager if you will), add the code that I originally provided.

Where ever it is needed do something like

 public class lo : $$anonymous$$onoBehaviour
 {
     public Game$$anonymous$$anager game$$anonymous$$anager;
     private bool sceneIsLoading = false;
 
     void OnCollisionEnter2D(Collision2D coll)
     {
         // do not try to load scene if sceneIsLoading is set
         if (!sceneIsLoading)
         {
             // set here so this block is never entered again
             sceneIsLoading = true;
             if (game$$anonymous$$anager != null)
             {
                 game$$anonymous$$anager.LoadNextScene();
             }
         }
     }
 }
avatar image sid4 TBruce · Oct 18, 2016 at 12:39 AM 0
Share

ok I will try this tomorrow and get back to you

thanks and I will be in touch I hope this works

Show more comments
avatar image TBruce sid4 · Oct 18, 2016 at 08:44 PM 0
Share

I just sent you a private message.

avatar image sid4 TBruce · Oct 18, 2016 at 08:56 PM 0
Share

message me here

or I can giv u my new email address

Show more comments
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

90 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 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 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 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 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 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

How do I trigger a game object to move a desired distance at a desired speed? 1 Answer

How to change button.spriteState.pressedSprite sprite by script - Unity 4.7.1 1 Answer

Using multiple materials on a 2d mesh 0 Answers

Problem with c# script 0 Answers

ObjectParameter Usage in a Custom Skybox 0 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