- Home /
Help with scene switching with a game object trigger.
I got a game object to act as a trigger to switch scenes after touching it it, the problem is that after the scene switches, the previous scene is still there and running, So when i move my player it ends up hitting the game object again creating another load of the scene. How do I go about this?? I heard i can destroy the object after but i dont know how to do so.
Answer by AlejandroBoss10 · Mar 07, 2018 at 05:54 PM
This is what I have for loading an entirely different scene. So you put a trigger on the object that is acting as the trigger. Attach this script. Type the name of the scene in the box in the inspector, be sure to check that the scene is spelled letter by letter and is in your build settings. Below is the script, checked and working. If you need more info, just say something.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class OnTriggerScene : MonoBehaviour {
public string loadLevel;
void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(loadLevel);
}
}
it works better than what i had before, but when i load the next level the lighting is messed up. The scene darkens, as if it hasn't finished loading. Any fix for this?
This is probably only an issue in the editor (You should make a build to confirm). To fix it in the editor in both of your scenes go to Window > Lighting > Lightmaps and disable auto and click bake. Both of your scenes :)
@dsabatier is absolutely correct. It's nothing with the script itself, but rather with Unity. The lighting in the newly loaded scene has not had the time to actually be baked (in other words, it hasn't really rendered). It should work fine if you make a build and test it out. It's just a problem in the Editor. When you build, if you look in the little window that shows, it says something about building lighting or something like that for a certain scene. Just follow what @dsabatier said and it should work.
Answer by dsabatier · Mar 06, 2018 at 08:51 PM
Can you post the code you're using to switch scenes? It sounds like you are perhaps loading the scene additively. Is that what you want?
You'll want to do something like this if not:
SceneManager.LoadScene("SceneName", LoadSceneMode.Single);
or if you really need to, just Destroy the object in the same call as the Load using Destroy(gameObject)
Answer by KingKamal · Mar 07, 2018 at 05:37 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour {
// Use this for initialization
void OnTriggerEnter(Collider ChangeScene)
{
if(ChangeScene.gameObject.CompareTag("Player"))
{
Application.LoadLevelAdditive(1); //1 is the build order it could be 1065 for you if you have that many scenes
}
}
}
As already mentioned by the accepted answer, you'll probably want to use Scene$$anonymous$$anager.LoadLevel(string scenename) ins$$anonymous$$d. I'm surprised to see Application.LoadLevel at all. If I'm not mistaken that has been deprecated for ~3 years now :)
Your answer
Follow this Question
Related Questions
how to link scenes ? 4 Answers
Fade In each level 1 Answer
Should i invest in level prefabs instead of scenes? 3 Answers
remembering variables between scenes 2 Answers
Objects disappearing and scripts not working in 2 scenes. Corrupt scenes? 1 Answer