Gustav
Gustav

Reputation: 33

Unity C#: change scenes by reading callback Boolean inside voide Update()

Struggling with a specific thing here. I need to change scenes when the calibration is successful. For that, I can use:

if (someVariable == true)
{
   SceneManager.LoadScene("MainScene");
}

The code that calls the calibration start is this:

private void Update()
        {
            if (Input.GetKeyDown(_startKey))
            {
                var calibrationStartResult = StartCalibration(
                    resultCallback: (calibrationResult) =>
                        Debug.Log("Calibration was " + (calibrationResult ? "successful" : "unsuccessful"))
                );

                Debug.Log("Calibration " + (calibrationStartResult ? "" : "not ") + "started");
            }
        }

I need a way to use that resultCallback inside the private void Update() in order to change scenes, because resultCallback returns either true or false, if the calibration of my VR system was successful. Also, here's what StartCalibration() returns:

public bool StartCalibration(Vector3[] points = null, System.Action<bool> resultCallback = null)

Q: How do I code this to use the boolean value of resultCallback to change the scene? So far it writes to the console whatever the result was, but I don't really need that output, if I can make it change scene automatically.

I'm not very experienced in C#.

I've tried the following, but, of course, it doesn't work:

private void Update()
        {
            if (Input.GetKeyDown(_startKey))
            {
                var calibrationStartResult = StartCalibration(
                    resultCallback: (calibrationResult) =>
                        Debug.Log("Calibration was " + (calibrationResult ? "successful" : "unsuccessful"))
            if (calibrationResult == true)
            {
                SceneManager.LoadScene("MainScene");
            }
                );

                Debug.Log("Calibration " + (calibrationStartResult ? "" : "not ") + "started");
            }
        }

Upvotes: 1

Views: 223

Answers (1)

Philipp Lenssen
Philipp Lenssen

Reputation: 9218

You almost had it -- the key is to define a second class-level property that persists the local value. Note I'm renaming calibrationResult to calibrated for ease of use. Try:

bool calibrated = false;
bool startedCalibration = false;

private void Update()
{
    if (Input.GetKeyDown(_startKey) && !startedCalibration)
    {
        startedCalibration = true;
        StartCalibration(
            resultCallback: (calibrated) =>
            {
                startedCalibration = false;
                print("Calibration was " + (calibrated ? "successful" : "unsuccessful");

                // Assign the local scope variable to the class property:
                this.calibrated = calibrated;
            }
        );
    }

    if (calibrated)
    {
        calibrated = false;
        SceneManager.LoadScene("MainScene");
    }
}

Another way to achieve this would be to just call SceneManager.LoadScene from within the resultCallback scope. This would also save on the repeat bool check of calibrated inside the Update. While a single Update check isn't lagging, you want to generally keep track of how much needed and unneeded things you do in these eternal loops. Good luck!

Upvotes: 2

Related Questions