- Home /
Restarting My Level Is Doubling The Game Objects In My Scene, But Why?
So to put it simply, I am making a game where you run around a maze and collect gems. To add a twist to this game I added traps in the game, one of them being holes that when you fall into restart the level. The problem is that when the level restarts, the number of gems in the maze doubles! (There are 15 gems in the first level and every time it restarts it adds 15) I don't know what is causing this but I don't want to take out a whole part of the game because of this glitch. Also, here's the level restarting script and the gem script in case I messed something up in one of them:
//Restarting Script//
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour { bool GameHasEnded = false; public float RestartDelay = 0f;
public void EndGame()
{
if (GameHasEnded == false)
{
GameHasEnded = true;
//Debug.Log("GameOver");
Invoke("Restart", RestartDelay);
}
}
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
//Gem Script//
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CollectObjects : MonoBehaviour { public static int objects = 0; public AudioSource ding; // Use this for initialization void Awake() { objects++; }
// Update is called once per frame
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
objects--;
gameObject.SetActive(false);
ding.Play();
}
}
}
//Number Of Gems Left Script//
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ObjectsLeft: MonoBehaviour { //public string nextLevel; public GameObject Trophy; public GameObject Trigger; public GameObject Trigger2; GameObject objUI; // Use this for initialization void Start() { objUI = GameObject.Find("ObjectNum"); } // Update is called once per frame void Update() { objUI.GetComponent().text = CollectObjects.objects.ToString(); if (CollectObjects.objects == 0) { //Application.LoadLevel(nextLevel); //Destroy(objToDestroy); Trophy.SetActive(true); Trigger.SetActive(true); Trigger2.SetActive(true); objUI.GetComponent().text = "All objects collected."; } } }
Answer by AaronBacon · Jun 30, 2019 at 10:12 AM
I think it's because you're using Application.LoadLevel. Try switching to SceneManager.LoadScene
using UnityEngine.SceneManagement;
void Update()
{
objUI.GetComponent().text = CollectObjects.objects.ToString();
if (CollectObjects.objects == 0)
{ //
SceneManager.LoadScene(nextLevel);
Destroy(objToDestroy);
Trophy.SetActive(true);
Trigger.SetActive(true);
Trigger2.SetActive(true);
objUI.GetComponent().text = "All objects collected.";
}
}
Be aware that the scene needs to be added to the build in the build settings, and can then be accessed by name or by ID. Also make sure you have UnityEngine.SceneManagement added to your usings
I am just going off other old questions like this btw, so I might be wrong: https://answers.unity.com/questions/37115/applicationloadlevel-problem.html