• 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 netherboss123 · Jan 23, 2015 at 09:02 PM · game

Load Level on collision

Hello! i have been wondering how i could change the scene on collision because i am making a 2d / 3d endless runner game and i need it to change from a 2d scene to a 3d scene on collision. any idea how to do this?

Comment
Add comment · Show 5
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 ChrPunx · Jan 23, 2015 at 09:30 PM 1
Share

Something like this

 void OnCollisionEnter(Collision col){
     if(col.collider.transform.tag == "Player"){
         LoadLevel();
     }
 }
 
 void LoadLevel(){
     Application.LoadLevel(SceneName);
 }

avatar image Simon-Larsen · Jan 23, 2015 at 09:56 PM 0
Share

Don't see the need for the "LoadLevel" method, but why not post as an answer? :)

avatar image netherboss123 · Jan 23, 2015 at 10:20 PM 0
Share

Is this java or C#?

avatar image InvincibleCat · Jan 23, 2015 at 10:25 PM 0
Share

It is C# but I think what you want is like having the camera behind the character and on collision, the camera goes like a 2d platformer game?

For example is it Agent Dash like, and on collision become like Canabalt?

avatar image netherboss123 · Jan 24, 2015 at 01:00 AM 0
Share

exactly! but it starts in 2d

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by theLucre · Jan 23, 2015 at 10:02 PM

If you're working with 2D Physics components you need the use those collision functions

 void OnCollisionEnter2D(Collision2D col){
      if(col.transform.tag == "Player"){
          Application.LoadLevel( "SomeScene" );
      }
  }
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
1

Answer by sdclark79 · Apr 02, 2017 at 10:04 AM

This old thread is the top of the list for this fix, so here's an updated-for-2017 answer:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 //Make sure to add this, or you can't use SceneManager
 using UnityEngine.SceneManagement;
 
 
 public class Lvl02_Exit : MonoBehaviour {
 
     void OnTriggerEnter(Collider other){
         //other.name should equal the root of your Player object
         if (other.name == "AdvancedPlayer") {
             //The scene number to load (in File->Build Settings)
             SceneManager.LoadScene (3);
         }
     }
 }

This script is attached to the Collider (Cube with MeshRenderer turned off) at the end of the level. Whenever the Player (in my case, it's named "AdvancedPlayer"... thanks UFPS!) runs into the Collider, it loads the next Scene.

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
1

Answer by thatvengeance · May 07, 2018 at 03:33 AM

So no one goes insane like I did, the original was for a 2D scene where as the updated code is for a 3D scene. All you have to change is:

 using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      //Make sure to add this, or you can't use SceneManager
      using UnityEngine.SceneManagement;
      
      
      public class Lvl02_Exit : MonoBehaviour {
      
          void OnTriggerEnter2D(Collider2D other){
              //other.name should equal the root of your Player object
              if (other.name == "AdvancedPlayer") {
                  //The scene number to load (in File->Build Settings)
                  SceneManager.LoadScene (3);
              }
          }
      }

All colliders need to be set to 2D.

,So no one goes insane like I did. The updated code was written for 3D. If you are doing 2D then you have to change it like so:

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      //Make sure to add this, or you can't use SceneManager
      using UnityEngine.SceneManagement;
      
      
      public class Lvl02_Exit : MonoBehaviour {
      
          void OnTriggerEnter2D(Collider2D other){
              //other.name should equal the root of your Player object
              if (other.name == "AdvancedPlayer") {
                  //The scene number to load (in File->Build Settings)
                  SceneManager.LoadScene (3);
              }
          }
      }

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 Cryo_Quantom · Sep 23, 2018 at 01:06 PM 0
Share

Thanks $$anonymous$$ate!!! This script just saved my life. Busy with a project and needed to implement this to switch from Title Sequence to $$anonymous$$ain $$anonymous$$enu, with some small tweaks it made a huge difference! Thanks a lot!!

avatar image
1

Answer by MrScottyPieey · Mar 29, 2019 at 01:26 AM

Updated 2019

void OnCollisionEnter2D(Collision2D coll) { //If it touches the player. if (other.name == "Player") { SceneManager.LoadScene("Test"); } },void OnCollisionEnter2D(Collision2D coll) { //If it touches the player. if (other.name == "Player") { SceneManager.LoadScene("Test"); } }

Updated 2019

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

how to limit jumping 1 Answer

Many issues 0 Answers

saving scene and game objects 1 Answer

Creating Fps Game Menu 2 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