Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
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
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
1

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
Add comment · 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.Scene$$anonymous$$anagement.Scene$$anonymous$$anager:LoadScene(Int32) SceneSwitch:On$$anonymous$$ouseDown() (at Assets/Scripts/SceneSwitch.cs:58) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(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 On$$anonymous$$ouseDown()
     {
         if (String.IsNullOrEmpty(sceneToLoad))  //you might want to add a check if the scene exists
         {
             Debug.Log("Invanlid Scene Name");
             return;
         }
         Scene$$anonymous$$anager.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.Scene$$anonymous$$anagement; 
 
 public string sceneToLoad;
 
 void On$$anonymous$$ouseDown()
 {
     if (String.IsNullOrEmpty(sceneToLoad))  //you might want to add a check if the scene exists
     {
         Debug.Log("Invanlid Scene Name");
         return;
     }
     Scene$$anonymous$$anager.LoadScene(sceneToLoad);    
 }

Thanks

alt text

screen-shot-2017-05-07-at-092101.png (13.1 kB)
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

7 People are following this question.

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

Related Questions

Can I use one script to toggle between scenes? 1 Answer

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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