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 YOLO_cakes · Mar 10, 2016 at 09:37 AM · c#2d game

How do I change level/scene?

I have searched all over the internet to try and find out how I can do this but I cannot find a solution anywhere. I am using C# and am making a 2D game. I want it so that when the player enters a box colider it teleports him to a new scene. I read that you now need to use SceneManager instead of LoadLevel and this is where I am confused. The script is fine with no errors in console however, when the character goes into the collider nothing happens.I will post my script below and if anyone could help me fix it that would be great!

using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour {

 private bool playerInZone;

 // Use this for initialization
 void Start () {
     playerInZone = false;
 }
 
 // Update is called once per frame
 void Update () {
     if (playerInZone) 
     { 
         SceneManager.LoadScene ("Menu");
     }
 }

 void onTriggerEnter2D(Collider2D other)
 {
     if(other.name == "Player")
     {
         playerInZone = true;
     }
 }

}

Comment
Add comment · Show 3
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 gjf · Mar 09, 2016 at 08:03 PM 1
Share

please try to format ALL of your code...

your problem is that you're expecting unity to call onTriggerEnter2D() upon collision but it's not going to. if you used OnTriggerEnter2D() on the other hand...

you need to pay special attention to the case.

avatar image YOLO_cakes gjf · Mar 10, 2016 at 09:47 AM 0
Share

So I have changed the script as you said but it is still not working? I am not sure if I missed something else that is obvious but I am not very experienced with C#.

Here is the new script:

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

public class LevelLoader : $$anonymous$$onoBehaviour {

 private bool playerInZone;

 // Use this for initialization
 void Start () {
     playerInZone = false;
 }
 
 // Update is called once per frame
 void Update () {
     if (playerInZone) 
     { 
         Scene$$anonymous$$anager.LoadScene ("02");
     }
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if(other.name == "Player")
     {
         playerInZone = true;
     }
 }

}

avatar image Ali-hatem YOLO_cakes · Mar 10, 2016 at 10:22 AM 0
Share

is ("02") a string value or you try to add int value & why do you use bool variable to load scene OnTriggerEnter2D is enough :

  void OnTriggerEnter2D(Collider2D other)
      {
          if(other.name == "Player")
          {
              Scene$$anonymous$$anager.LoadScene ("$$anonymous$$enu");
          }
      }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by prakash.jadhavjp · Mar 10, 2016 at 10:18 AM

write a debug.Log in the OnTriggerEnter2D() to ensure it is getting triggered. make sure that at least one the box or the player has rigidbody2d component to it . For a trigger to work at least one has to have the rigidbody2d component

Please try to make the player as DontDestroyOnLoad() ; so that your player gameobject is in the secound secene also .

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D movement along vector 1 Answer

Z position moving in 2D space 1 Answer

I can't seem to rotate my character depending on what side he is running to (2D sidescroller) 0 Answers

Reach other object animator when collide and play animation 1 Answer

C# delaying command 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