- Home /
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 () {
}
}
Thanks
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.
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,
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).
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