ObjectDisposedException: SerializedProperty Options.Array.data[3] has disappeared!

Hi, happy new year! :slight_smile:

Im facing this problem

As you can see, my Quiz Manager has some Button params. Im doing quiz with different number of choices for different questions so I building my flow as there is only 1 button blueprint. Then base on number of choice I gave for each question, I will clone as the same number I need.

That’s why when I play my continuous question, every clone button need to be destroy and the options list in Quiz Manager need to remove them from the list.

    public void HandleContinue(){
         QnA.RemoveAt(CurrentQuestion);
      
        for (int i = Options.Count - 1; i > 0; i--){
            Destroy(Options[i]);
             Options.RemoveAt(i);
        }
        Options[0].GetComponent<Image>().color = Color.white;

        if (numberQuestion < 10){
        generateQuestion();

         if (currentScene.name == "QuizTest"){
               QuizTimer.GetComponent<WarningTimer>().Warning();
        }
        }
        numberQuestion++;
    }

the generateQuestion() will generate questions and answers as cloning number of buttons I need to my question

 void generateQuestion()
    {
        if (QnA.Count > 0)
        {

         
            CurrentQuestion = Random.Range(0, QnA.Count);
            QuestionTxt.text = QnA[CurrentQuestion].Question;
            Vector2 firstChoicePosition = Options[0].GetComponent<RectTransform>().anchoredPosition;

            for (int i = 0; i < QnA[CurrentQuestion].Answers.Length - 1; i++)
            {
                GameObject newButton = Instantiate(btnPrefab);
                RectTransform rectTransform = newButton.GetComponent<RectTransform>();
                TextMeshProUGUI buttonText = newButton.GetComponentInChildren<TextMeshProUGUI>();

                if (rectTransform != null && buttonText != null)
                {
                    buttonText.text = QnA[CurrentQuestion].Answers[i]; // Set button text

                    // Adjust button size based on text length
                    AdjustButtonHeightBasedOnText(rectTransform, buttonText);

                    rectTransform.anchoredPosition = new Vector2(firstChoicePosition.x, firstChoicePosition.y - 115f);

                    // Update position for the next button
                    firstChoicePosition = new Vector2(firstChoicePosition.x, rectTransform.anchoredPosition.y);
                }

                newButton.SetActive(true);
                newButton.transform.SetParent(btnPrefab.transform.parent.transform, false);
                Options.Add(newButton);
            }

            SetAnswers();
        }
    }

Here is my demo.

The ContinueBtn will call HandleContinue. Im thinking that maybe that Im click the continue button too fast that it cannot handle well. You can see in my last try, it works normal. But I want to get rid all of the errors so please help me to enhance my work. Many thanks.

I have the same issue. On runtime i add gameObjects to a list, when they enter a trigger. When the objects leave the triger or the gameobject get killed( meele game). it gets removed from the list.
The error in my case:
ObjectDisposedException: SerializedProperty enemysInRange.Array.data[2] has disappeared!
apears only when the component where the list is stored is visible in the Inspector.
When i have anything other selected while runing the game, it dont happens.
So maybe a fix for it? :wink:

1 Like