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