Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by asimov · Jul 04, 2012 at 12:04 AM · arrayobjectdestroyremoveelement

How to remove an object from an array once it has been destroyed

Hi,

I'm working on a script that is attached to the Camera that fades and then shortly after, destroys the object moving towards it. Unfortunately, I'm getting an error just after the object has been destroyed:

 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.
 FadingTestCam.Update ()

I'm guessing I have to remove the object from the 'Enemies' array ince it has been destroyed, but am not sure how. The script is shown below.

Thanks

 private float Dist;
 
 public float FadeDistance;
 
 public GameObject[] Enemies;
 
 void Start()
 {
     Enemies = GameObject.FindGameObjectsWithTag("Enemy");
 }
 
 void Update()
 {
      foreach (GameObject enemy in Enemies)
     {
         Dist = Mathf.Abs(transform.position.z - enemy.transform.position.z);
 
         if (enemy.collider.tag == "Enemy" && Dist < FadeDistance)
         {
             //fade out (level of alpha 0 = transparent, time to take to fade)
             iTween.FadeTo(enemy, 0.0f, 0.2f);
 
             StartCoroutine(DestroyObject(enemy));              
         }
     }
 }
 
 IEnumerator DestroyObject(GameObject e)
 {
     yield return new WaitForSeconds(1);
 
     Destroy(e);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jul 04, 2012 at 12:28 AM

You could avoid this error by simply checking if the enemy exists - when a game object is destroyed, all of its references become null (don't know how they do this!):

void Update()
{
    foreach (GameObject enemy in Enemies){
        if (enemy){ // ignore dead enemies
            Dist = Mathf.Abs(transform.position.z - enemy.transform.position.z);
            if (enemy.collider.tag == "Enemy" && Dist < FadeDistance)
            {
                //fade out (level of alpha 0 = transparent, time to take to fade)
                iTween.FadeTo(enemy, 0.0f, 0.2f);
                Destroy(enemy, 1); // you don't need a coroutine for this!
            }
        }
    }
}
Removing dead enemies may seem more adequate, but the time spent to recreate the array will be much greater than the time wasted skipping dead enemies - thus I think this is the better solution.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asimov · Jul 04, 2012 at 12:41 AM 0
Share

That's great Aldo, agree with you that ignoring the dead enemies is better than re-creating the array - much appreciated! :)

D'oh, completely forgot about the second parameter of Destroy().

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why are Objects not getting destroyed? 0 Answers

Removing objects from an array 2 Answers

Remove object from Array in javascript 1 Answer

Swapping elements of an array 1 Answer

How to remove objects from a list ? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges