Counting scenes

Hello all,
Is there a way via code to check how many times a specific scene is loaded? I’d like to use the number of times one of my scenes is played in order to trigger an event. It seems like this was a bit easier to do in earlier releases in unity, but every method I try to use seems to yield nothing.
This is what my code looks like:

public class ToTheLivingRoom : MonoBehaviour {
    public bool thermoGet;
    public bool compGet;
    public bool bearGet;
    public Flowchart flowchart;
    public int loadTimes= 0;
    Scene scene = SceneManager.GetSceneByName("Office");
    public bool sceneActive; 
    
    // Use this for initialization
    void Start () {
        flowchart = GameObject.FindObjectOfType<Flowchart>();
        bearGet = flowchart.GetVariable("BearGet");
        thermoGet = flowchart.GetVariable("ThermoGet");
        compGet = flowchart.GetVariable("CompGet");
        
    }

    void Awake()
    {
        sceneActive = true;   
    }
    // Update is called once per frame
    void Update () {
        
        //if (scene.name("Office"))
        //{
        //    sceneActive == true;
        //}
        if (sceneActive == true)
        {
            loadTimes += 1;

            Debug.Log("The Office has loaded this many times:" + 1);
        }

        if (loadTimes == 3)
        {
            if (bearGet && thermoGet && compGet == true)
            {
                flowchart.ExecuteBlock("A warning (Copy)");
            }
        }
        
		
	}
}

As a note I am using fungus and referencing it in code, so I’m not sure if it’s the two systems not playing nice or something I’m missing.

Thanks!

Hello. There are several ways to do it. But first thing to know is: what exactly is the problem. By default, when a scene is unloaded, everything in that scene is destroyed. For example, when you go from scene A to scene B, scene A is unloaded, and scene B is loaded. Everything in scene A is gone. Now if you go back to scene A, everything returns to its initial state, includes GameObject positions, variable values… etc.

So, if you store loadTimes in a usual way, by creating a field in a normal MonoBehaviour script, then this information is gone whenever scene changes. You need something that can persists between scenes so that you can store loadTimes without it returning to initial value whenever scene changes.

There are several ways to keep data between scenes. I haven’t use Fungus before so I don’t know if it has own feature that’s specific to this problem? you might need to look for it yourself. But all you need to solve is this persistence problem. One way to solve it is using Static. You can try create a LoadTimesCounter script:

public static class LoadTimesCounter {

	public static int officeLoadTimes;

}

This script doesn’t need to be attach to any GameObject. Then in your office script, do something like:

using UnityEngine;

public class Office : MonoBehaviour {

	// Use this for initialization
	void Start () {
		LoadTimesCounter.officeLoadTimes++;
		Debug.Log("Office has loaded " + LoadTimesCounter.officeLoadTimes + " times");
	}
}