Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by alsophanie, Dec 31, 2023.

  1. alsophanie

    alsophanie

    Joined:
    Dec 3, 2023
    Posts:
    21
    Hi, happy new year! :)

    Im facing this problem
    upload_2024-1-1_0-24-42.png

    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.

    Code (CSharp):
    1.     public void HandleContinue(){
    2.          QnA.RemoveAt(CurrentQuestion);
    3.      
    4.         for (int i = Options.Count - 1; i > 0; i--){
    5.             Destroy(Options[i]);
    6.              Options.RemoveAt(i);
    7.         }
    8.         Options[0].GetComponent<Image>().color = Color.white;
    9.  
    10.         if (numberQuestion < 10){
    11.         generateQuestion();
    12.  
    13.          if (currentScene.name == "QuizTest"){
    14.                QuizTimer.GetComponent<WarningTimer>().Warning();
    15.         }
    16.         }
    17.         numberQuestion++;
    18.     }
    19.  
    the generateQuestion() will generate questions and answers as cloning number of buttons I need to my question
    Code (CSharp):
    1.  void generateQuestion()
    2.     {
    3.         if (QnA.Count > 0)
    4.         {
    5.  
    6.          
    7.             CurrentQuestion = Random.Range(0, QnA.Count);
    8.             QuestionTxt.text = QnA[CurrentQuestion].Question;
    9.             Vector2 firstChoicePosition = Options[0].GetComponent<RectTransform>().anchoredPosition;
    10.  
    11.             for (int i = 0; i < QnA[CurrentQuestion].Answers.Length - 1; i++)
    12.             {
    13.                 GameObject newButton = Instantiate(btnPrefab);
    14.                 RectTransform rectTransform = newButton.GetComponent<RectTransform>();
    15.                 TextMeshProUGUI buttonText = newButton.GetComponentInChildren<TextMeshProUGUI>();
    16.  
    17.                 if (rectTransform != null && buttonText != null)
    18.                 {
    19.                     buttonText.text = QnA[CurrentQuestion].Answers[i]; // Set button text
    20.  
    21.                     // Adjust button size based on text length
    22.                     AdjustButtonHeightBasedOnText(rectTransform, buttonText);
    23.  
    24.                     rectTransform.anchoredPosition = new Vector2(firstChoicePosition.x, firstChoicePosition.y - 115f);
    25.  
    26.                     // Update position for the next button
    27.                     firstChoicePosition = new Vector2(firstChoicePosition.x, rectTransform.anchoredPosition.y);
    28.                 }
    29.  
    30.                 newButton.SetActive(true);
    31.                 newButton.transform.SetParent(btnPrefab.transform.parent.transform, false);
    32.                 Options.Add(newButton);
    33.             }
    34.  
    35.             SetAnswers();
    36.         }
    37.     }


    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.
     
  2. almghandi

    almghandi

    Joined:
    Jul 4, 2020
    Posts:
    4
    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? ;)