- Home /
resetting count variable on scene change?
I am making a game that has 1 room and that room is duplicated across 10 scenes. There are two players that must choose the same door. Once chosen it adds 1 to that doors count and loads the next scene. The problem is that when it loads the next scene it also returns that doors count value back to 0. I need a final total of each doors count at the end of the 10 scenes. So my question is how do I change the code to make it keep count and not reset back to 0 after each scene?
I have tried creating a static variable to hold the count and that did not work unfortunately.
Thanks for the help in advance. :) Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class door_trigger_count : MonoBehaviour
{
public string nextLevel;
private int count;
private int count2;
private int totalRed;
private int totalBlue;
public bool redDoor;
public bool blueDoor;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (count == 1 && count2 == 2)
{
if (redDoor == true)
{
totalRed++;
}
if (blueDoor == true)
{
totalBlue++;
}
Debug.Log("red " + totalRed);
Debug.Log("blue " + totalBlue);
SceneManager.LoadScene(nextLevel);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "player1")
{
count = 1;
}
if (other.gameObject.name == "player2")
{
count2 = 2;
}
}
}
Your answer
Follow this Question
Related Questions
Using parameters of activeSceneChanged? 0 Answers
Change scene by gui-text counter 0 Answers
Loading Unity Scene Asynchronously stacking hierarchy window with scene names. 0 Answers
Loading a scene based on a generated number 2 Answers
FixedUpdate is not called after loading a level for the second time 1 Answer