Search Unity

Problem with "'GameObject' has been destroyed but you are still trying to access it."

Discussion in 'Scripting' started by schismatic, Aug 14, 2011.

Thread Status:
Not open for further replies.
  1. schismatic

    schismatic

    Joined:
    Jul 27, 2011
    Posts:
    10
    Hello,

    i have a problem with destroying objects.
    For example i have a bullet which cast "OnCollisionEnter" a SphereCastAll and than damage all Objects in Range with the Tag "Enemy" by calling their "TakeDamge" function.

    Code (csharp):
    1.  
    2. RaycastHit[] Targets = Physics.SphereCastAll(gameObject.transform.position, 1f, gameObject.transform.up);
    3. foreach (RaycastHit rchHit in Targets) {
    4.     if (rchHit.collider.tag == "Enemy") {
    5.         if (rchHit.collider.gameObject != null) {
    6.             rchHit.collider.GetComponent<EnemyAi>().TakeDamage(fDamage);
    7.         }
    8.     }
    9. }
    10.  
    My problem is, if 2 bullets hits the enemy at the same time and the first one will force the enemy to be destoryed i get this error Message:

    Code (csharp):
    1. MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    But i check if it is not null :confused:

    Here is the "TakeDamage" function:
    Code (csharp):
    1.  
    2. public void TakeDamage(float Damage) {
    3.   // Do something
    4.   Destroy(gameObject);
    5. }
    6.  
     
    BMRG14 likes this.
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Maybe you should put this in your TakeDamage function.

    Code (csharp):
    1.  
    2. public void TakeDamage(float Damage)
    3. {
    4.     if (gameObject != null)
    5.     {    
    6.         // Do something  
    7.         Destroy(gameObject);
    8.     }
    9. }
    10.  
     
  3. schismatic

    schismatic

    Joined:
    Jul 27, 2011
    Posts:
    10
    That's it :) Thank you
     
  4. HernandoNJ

    HernandoNJ

    Joined:
    May 13, 2018
    Posts:
    75
    I had the same issue.

    In my case, basically it was due to d&d (drag & drop) an object from hierarchy into the inspector slot. When the object is destroyed, it is not possible to create a new one.

    The solution is d&d directly from Prefab folder (or as prefab, in the project tab), not from hierarchy.

     
    Last edited: Feb 6, 2020
  5. Ushing

    Ushing

    Joined:
    Jul 6, 2018
    Posts:
    1
    Thanks
     
    firebird721 and HernandoNJ like this.
  6. Nitish_Thampy

    Nitish_Thampy

    Joined:
    May 11, 2019
    Posts:
    2
    thank you so much
     
    HernandoNJ likes this.
  7. felipe_slv

    felipe_slv

    Joined:
    Jan 3, 2020
    Posts:
    4


    I just came to this answer two minutes before I read your reply, and that's what solved the problem for me as well.
     
  8. ludsbradva

    ludsbradva

    Joined:
    Jul 20, 2020
    Posts:
    1

    Thanks, mate! I was struggling with this one :)
     
    HernandoNJ likes this.
  9. leenah911

    leenah911

    Joined:
    Feb 3, 2020
    Posts:
    1
    It worked, Thanks !:)
     
    HernandoNJ likes this.
  10. VRKid

    VRKid

    Joined:
    Jul 1, 2016
    Posts:
    42
    thank you so much.
     
    HernandoNJ likes this.
  11. gamdevlooper

    gamdevlooper

    Joined:
    Sep 4, 2020
    Posts:
    2
    It worked man thnx
     
  12. CaresizMfci

    CaresizMfci

    Joined:
    Apr 12, 2021
    Posts:
    1
    Finallyyy!!!!! thank you soo much
     
    HernandoNJ likes this.
  13. Sukarayam_Janjua

    Sukarayam_Janjua

    Joined:
    Nov 9, 2020
    Posts:
    1
    Thanks a lot!!
     
    HernandoNJ likes this.
  14. endiuc

    endiuc

    Joined:
    Nov 1, 2021
    Posts:
    1
    Muchas gracias!!!! :)
     
    HernandoNJ likes this.
  15. SapKnight

    SapKnight

    Joined:
    Apr 12, 2022
    Posts:
    1
    吨thank you!!its very useful
     
    HernandoNJ likes this.
  16. InDza

    InDza

    Joined:
    Jul 4, 2022
    Posts:
    1
    i have the same problem, but no one of above solution was efective to me.
    in my case, the problem is in the rigidbody2d on player object.
    the error appear when i m go to main menu from gameplay then go back to gameply then jump gameror2.png gameror.png .
    The error refference to input system also.
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
  18. zerored

    zerored

    Joined:
    Jan 24, 2016
    Posts:
    5
    Verifica que el objeto al cual esta llamando no se destruya. Por ejemplo si instancias un objeto dentro de un padre pero su padre se destruye el hijo ira una vez pero la segunda ya no tendrá a donde ir.
     
    Last edited: Apr 16, 2023
Thread Status:
Not open for further replies.