Is There A Reason This Code Is Making MY Game Crash

Hey, so i have a toggle button for if you want to set quest goal complete pops to be active and im using the code like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ToggleQuestUpdates : MonoBehaviour
{
   public Toggle toggle;

    void Start()
    {
        toggle.isOn = QuestComeIn.QuestUpdates;
    }

    public void Switch()
    {
        if(toggle.isOn == true)
        {
            toggle.isOn = false;
            QuestComeIn.QuestUpdates = false;

        }
        else
        {
            toggle.isOn = true;
            QuestComeIn.QuestUpdates = true;
        }
    }
}

i have the bool in another script as thats where all the other things are related to the quest and they all get saved together.

I have the function called on value changed and when i click it in game it crashes

You need to replace line 12, 19 and 25 with:
https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.UI.Toggle.html#UnityEngine_UI_Toggle_SetIsOnWithoutNotify_System_Boolean_

Otherwise your function gets called infinitely because it sets the toggle on or off which triggers your function which sets it on/off which triggers your function, and so on forever.

2 Likes

Amazing catch Praetor… seems like every non-trivial event-driven system always ends up with this same old rotten fishy “code smell.”

thank you so much i would have never got that :slight_smile:

1 Like