#A listener that when called, crashes Unity

1 messages · Page 1 of 1 (latest)

rich oak
#

Hello, I've a weird problem/bug.
Every time this listener starts working, it crashes, even before the wrapTasks function being called, which wraps all data from the tasks ui to a List<Task> to be saved.
It just stops Unity from responding to anything, just stuck on 1 frame, I couldn't debug it in anyway.

I use (2021.3.111f LTS)

taskToggle.onValueChanged.AddListener(delegate
{
    Debug.Log("wrapping");
        List<Task> wrappedTasks = wrapTasks();
    Debug.Log("Now fr starting to save :)");
    tasksData.Save(wrappedTasks);
    Debug.Log("Saved data through .onValueChange .addlistener delegate!!!!!! why");
});
    List<Task> wrapTasks()
    {
        Debug.Log("Began wrapping");
        List<Task> wrappedTasks = new List<Task>();
        
        Debug.Log("Just before wrapping foreach");
        
        foreach (Transform child in tasksPanel.transform)
                {
                        Debug.Log(child.name);
                        GameObject obj = child.gameObject;

                        Task task = new Task();

                        GameObject taskGameObject = Instantiate(taskTemplate, tasksPanel.transform);
                        Transform taskTransform = taskGameObject.transform.GetChild(0);
                        TMP_InputField taskInputField = taskTransform.transform.GetChild(1).gameObject.GetComponent<TMP_InputField>();
                        TMP_Text taskText = taskInputField.GetComponentInChildren<TMP_Text>();
                        Toggle taskToggle = taskTransform.GetComponent<Toggle>();

                        task.done = taskToggle.isOn;
                        task.task = taskText.text;

                        wrappedTasks.Add(task);

                }
       
        Debug.Log("After the foreach wrapping and before returning the wrapping");
        
        return wrappedTasks;
    }

Thanks in advance.

#

"tasksPanel" variable is referencing this panel:

bleak heart
#

It looks like you're looping over all children of tasksPanel, and inside that loop you instantiate a new one, so this loop can never end

#

You take one step towards the goal and then the goal takes one step back

rich oak
bleak heart
#

Yeah this one's definitely a sneaky infinite

#

Not quite as noticeable as a while(true)

novel dragon
#

Sometimes we need other people to help us find the obvious lol

rich oak
#

Going to debug rn, and I'll close the thread after making sure everything is working as intended.
I really appreciate it.