What is wrong here: 'GameObject' has been destroyed but you are still trying to access it.

I am working with a piece of code for a card game and get the error on the second print statement indicating that it is the selectedTemplCards[2].tag that is the problem, but it works on the first print statement?

…I do not understand why this is happening and were it is destroyed.

I get the following output:

1 - selectedTemplCards[s].tag: PRINCESS (this is correct)

...here is the full error statement from the second print statement:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
TemplateScript+<FixTimeToDealTemplateCards>c__Iterator3.MoveNext () (at Assets/Script/GameBoard/TemplateScript.cs:309)

While i do understand the error i do not understand why, and were, it happen.

int s = selectedTemplCards.Count - 1;
print ("1 - selectedTemplCards[s].tag: " + selectedTemplCards[2].tag);

while (s > -1) { 
    print (s);

   yield return newWaitForSeconds(secondsRateToTheDeal);

   //Save the final position
   Vector3 finalCardPos = new Vector3(theDealTemplCardList[s].PosX, theDealTemplCardList[s].PosY, theDealTemplCardList[s].PosZ);

   float rotZ = theDealTemplCardList[s].RotZ;
   bool faceBackUp = theDealTemplCardList[s].BackSideUp;

   //Make sure the card has the highest sort order during dealing
   GameEngineScript_gameEngineScript = gameObject.GetComponent<GameEngineScript>();

   print ("2 - selectedTemplCards[s].tag: " + selectedTemplCards[2].tag); <<<<ERROR HERE

   Singleton.Instance.master_GameObject_List =          _gameEngineScript.RefreshSortOrderAndZandPutObjectOnTop(Singleton.Instance.master_GameObject_List, selectedTemplCards[s]);

   StartCoroutine(ActualDealTheTemplateCards(selectedTemplCards[s], finalCardPos, rotZ, faceBackUp, theTimeRate));

   audio.PlayOneShot(dealSound);
   s--;
 }

Can’t see amything wrong there. Do you have any other running scripts that could be modifying your array at runtime?

Use the mono-develop debugger to see when the exception is called, then you can inspect the state of the gameobject and hopefully see what wrong

From just simply reading the code, I’d say that it’s iterating over your data container once and when it hits one of these lines of code it either messes up your indexing, or it completely destroys it:

Singleton.Instance.master_GameObject_List =          _gameEngineScript.RefreshSortOrderAndZandPutObjectOnTop(Singleton.Instance.master_GameObject_List, selectedTemplCards[s]);
   StartCoroutine(ActualDealTheTemplateCards(selectedTemplCards[s], finalCardPos, rotZ, faceBackUp, theTimeRate));

Is your container a list or an array? When you sort your array/list after placing the card out, does it ever take any cards OUT of the data container? This could be your problem. You have to be really careful when sorting data containers like that. You may need to start the sort from the last index of the container, and end with 0. It’s hard to see without the full code for your sorting, but no gameobjects are being destroyed in the code you’ve given us.

The ActualDealCards Coroutine could be doing it as well. If you Destroy() a card when it is dealt, or if you’re sorting the cards again after the actual dealing has been done, etc… My guess is that the error is coming from your sorting algorithm. That would be the first place I’d check.

Dameon_ Thanks! I added a coroutine with a pause for one second and now it works :slight_smile: …as I start this process and at the same time did the sorting, which involve the same Game Objects, that seems to have been the problem.

I think that was the problem I was dealing my cards, takes time, and at the same time did the sorting. Thanks.

Thanks for this comment. I have never used this before and now i learned it. Great :slight_smile:

Hmm… There may need to be a bit of refactoring in your code in the future… It’s kinda scary that you have to wait with a coroutine before dealing. You should call Deal() at the end of Sort(), that way you know for a fact that you won’t run into this again. The main problem is that it may take more than 1 sec to sort, which is leaving potential for this bug to pop up from time to time. I would add a bool paramater to Sort() called dealAfter. At the very end of the function, check to see if dealAfter is true. If true, Deal(). This way we know for a FACT that everything is sorted before we deal. It’s not a HUGE issue if your game is running at the moment, but (from reading what you stated above) it still looks like there may be room for it to pop back up later on. Remember man, our main goal is to Squash the bugs, not band-aid them! :slight_smile:

Epictickle thanks. I did think about exactly that but the sort always have the same amount of objects. However, i may correct this going forward.