Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by TheRealKuha · Jul 16, 2015 at 08:14 AM · 2dscenescene-loadingoncollisionenter

Changing Scenes on 2D Collision

So I'm new to scripting and I have a 2D game. I want to change scenes when my player collides with another quad. They both have 2DBox Colliders, the quad's box collider is checked as a trigger, the player is tagged "Player" and this script is attached to the quad.

 using UnityEngine;
 using System.Collections;
 
 public class CollisionSceneChange : MonoBehaviour
 {
     public string level = "testingscene2";
 
     // Use this for initialization
         void OnCollisionEnter2D (Collision2D Colider)
     {
         if(Colider.gameObject.tag == "Player");
         Application.LoadLevel(level);
     }
 }

it gives me no syntax errors and the game runs but it doesn't work. I tried with OnTriggerEnter2D but that doesn't work either. Can somebody please help?

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 TheSteiner · Dec 19, 2016 at 12:35 PM 0
Share

You have no idea what I've been fighting with this. Your code, with some changes, has been the one that has finally served me. Thank you!

4 Replies

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

Answer by zach-r-d · Jul 16, 2015 at 10:46 AM

Please do all of the following:

  1. Delete the semicolon at the end of the if statement (line 11).

  2. Change the function back to OnTriggerEnter2D again. OnCollision*** is only called for two non-triggers.

  3. Make sure the player object is tagged Player.

  4. Make sure that collisions are enabled between the layers that the player and the scene change trigger are on.

Comment
Add comment · Show 8 · 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 Hellium · Jul 16, 2015 at 10:52 AM 1
Share

5. $$anonymous$$ake sure the object which must collide has a Rigidbody component

avatar image zach-r-d · Jul 16, 2015 at 11:13 AM 0
Share

Good catch! Should probably edit to say Rigidbody2D though.

avatar image TheRealKuha · Jul 16, 2015 at 11:18 AM 0
Share

It still doesn't work. I tried everything from the last answer too. I did everything as you said as well but no results. And by number 4. you mean that I set the objects in a way their colliders will touch? If you mean that yes I did that too. $$anonymous$$aybe it's because my player is using rays to detect collision and slopes so the movement is smooth. I really don't know anymore. If it helps I'll post the scripts my player uses but I can't here because of the character limit.

avatar image zach-r-d · Jul 16, 2015 at 11:23 AM 0
Share

Number 4 means looking at the layer the player is in (the one next to the tag), looking at the layer the trigger is in, then going to Edit -> Project Settings -> Physics 2D and the checkbox indicated by those two layers is checked. Without posting code, could you briefly describe how the player uses rays to detect collision, and equally importantly, how it responds to those collisions?

avatar image TheRealKuha · Jul 17, 2015 at 10:50 AM 0
Share

Nah dude I got it working finally, the script was alright everything I typed was good I did everything you said but and it wasn't working untill I saw the Rigidbody comment. And I added the rigidbody2D to the object that has the script on and it works :D thanks for the help man! You really saved me from a lot of rage.

Show more comments
avatar image
1

Answer by Hexer · Jul 16, 2015 at 11:17 AM

 void OnColliderEnter2D (Collider2D collider)
     {
     if(collider.tag == "Player")
     Application.LoadLevel("testingscene2");
     }
 

I wouldn't know why you would put "testingscene2" as a string to level. but your code is fine.

make sure your quad has a rigidbody2D attached to it. and set the gravity Scale to zero. That is if you want to not make use of gravity for that object.

Make sure the trigger is enabled on just 1 of the objects. (not 2, just 1)

EDIT : Add Debug.Log("its Colliding!); to make sure it is working.

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 TheRealKuha · Jul 17, 2015 at 10:51 AM 0
Share

Yeah man I got it working it was the rigidbody2D component that was missing. I added it to the player and now it works.

avatar image
0

Answer by Johnz1234 · Jul 16, 2015 at 10:39 AM

 void OnTriggerEnter2D(){
 Application.LoadLevel(2);
 }
 and check if the object where the script is put have a collider and set it to trigger :D
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 basim15 · Aug 16, 2016 at 07:01 AM

using UnityEngine; using System.Collections;

public class levelchange : MonoBehaviour {

 void OnColliderEnter2D (Collider2D collider)
 {
     if(collider.tag == "Player")
         Application.LoadLevel("level1");
         Debug.Log("its Colliding!");
 }

}

this is my code but nothing is happening i have no clue why, i have a box collider with is trigger on my object and i've also loaded the scenes in build settings

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

I am making a 2D game and I need to repeat the sound from 1 to 2 scenes. (to keep it going) sorry for my english 1 Answer

How to efficiently populate scene with GameObjects 1 Answer

How to use one object selected from previous scene to the current scene ? 1 Answer

Elevator Motion Going Up Smooth, Going Down Not Smooth 1 Answer

[SOLVED] Sprite showing up in "Scene", but not in "Game" 11 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