• 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
Question by jimm84 · May 05, 2017 at 08:44 AM · c#collider2dscene-loadingscene-switchingmousedown

Transition between levels using 2d colider works once but not repeatidly.

Hello, hoping some one could help. I have 2 levels with a static background. Ontop of these static backgrounds sits a collider that when its clicked, takes you through to the second room and vice-versa. It works once on the first room but not the second. Do I need to declare the 2d collider object from the script? I have already added it as a component to the object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement; 
 
 public class SceneSwitch : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         //SceneManager.LoadScene (Random.Range(0, 6)); make a random scene load!
         //SceneManager.LoadScene (2);
         Scene scene = SceneManager.GetActiveScene();
         Debug.Log("Active scene is '" + scene.name + "'.");
         
     }
         
     //public class ExampleClass : MonoBehaviour {
     //    void OnMouseDown() {
     //        Application.LoadLevel("hall");
 
     //    }
     //}
 
     // Bedroom Doors
 
     void OnMouseDown()
     {
         Debug.Log ("Stop that, it tickles!");
         Application.LoadLevel("hall");
     }
 
 
 
     void OnMouseDown2()
     {
         Debug.Log ("shush!");
         Application.LoadLevel("scene1test");
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

alt text

Thanks

screen-shot-2017-05-03-at-162855.png (34.9 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by PizzaPie · May 05, 2017 at 09:19 AM

Well OnMouseDown() is an event trigger so it is called when a collider (or GUI) is clicked on the other hand OnMouseDown2() is not so it is never called.

 int hallSceneIndex;
 int scene1testIndex;
 
 void Awake(){
 hallSceneIndex = SceneManager.GetSceneByName("hall").buildIndex;
         scene1testIndex = SceneManager.GetSceneByName("scene1test").buildIndex;
 }
 
 void OnMouseDown()
     {
         if(SceneManager.GetActiveScene().buildIndex == hallSceneIndex)
             SceneManager.LoadScene(scene1testIndex);
         else
             SceneManager.LoadScene(hallSceneIndex);
     }

Use this script instead, makes sure it is attached to both triggers.

Comment
BaldBeardedMonk

People who like this

1 Show 4 · 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 jimm84 · May 06, 2017 at 03:43 PM 0
Share

Hello Pizza pie! I'm afraid this error comes back.

Cannot load scene: Invalid scene name (empty string) and invalid build index -1 UnityEngine.SceneManagement.SceneManager:LoadScene(Int32) SceneSwitch:OnMouseDown() (at Assets/Scripts/SceneSwitch.cs:58) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

I have also attached this to both triggers, just a question. Will this approach also be straight forward for me to add extra levels in the future?

Thanks,

avatar image PizzaPie jimm84 · May 06, 2017 at 05:30 PM 0
Share

Well you didn't mention above anything about any errors... anyway to fix it you need to open Build Settings (File->Build Settings for windows) and add your scenes in the list -> Scenes in Build. Then you need to make sure that the names in line 5 and 6 inside the GetSceneByName are exactly the same as the names of the scenes. As if this is gonna be usable for more scenes probably not. Use something like this->

 public string sceneToLoad;
 
     void OnMouseDown()
     {
         if (String.IsNullOrEmpty(sceneToLoad))  //you might want to add a check if the scene exists
         {
             Debug.Log("Invanlid Scene Name");
             return;
         }
         SceneManager.LoadScene(sceneToLoad);    
     }

To set this up add this on the object you want to change the scene on click and fill the field sceneToLoad on the inspector with the name of the scene you want to load. (again make sure that the scenes are added on the Scenes In Build and the names match).

avatar image jimm84 PizzaPie · May 07, 2017 at 08:46 AM 0
Share

Hello PizzaPie, I already have the levels added to the builder. Still running into issues and unsure on how to get around it. I have attached your script - part 2. It may be worth mentioning that these levels that i'm trying to get to access each are 3 and 4 on the list if that is of any consequence? It has returned an new error.

Assets/Scripts/SceneSwitch.cs(69,7): error CS1525: Unexpected symbol string', expecting class', delegate', enum', interface', partial', or `struct'

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement; 
 
 public string sceneToLoad;
 
 void OnMouseDown()
 {
     if (String.IsNullOrEmpty(sceneToLoad))  //you might want to add a check if the scene exists
     {
         Debug.Log("Invanlid Scene Name");
         return;
     }
     SceneManager.LoadScene(sceneToLoad);    
 }

Thanks

alt text

screen-shot-2017-05-07-at-092101.png (13.1 kB)
Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

UI canvas or panel that only appears once per game session? 1 Answer

Multiple Cars not working 1 Answer

After loading scene raycast2D script doesn't detect all clicks 0 Answers

Distribute terrain in zones 3 Answers

Multiple EvenetSystems in Scene - only have 1 after searching though 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