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.
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)
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);
}
}
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
}
}
}