Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Ranth · Jun 02, 2015 at 10:41 AM · arraydestroydestroy objectarraylist

Checking if first item in array has been destroyed

Hello and thanks for helping. I am making an RTS in which the players units are selected and then I select enemy units for them to destory. The enemy units get placed in an array called updateEnemyList. Each player unit has a function called Attack() which then sorts the list to put the closest enemy in updateEnemyList[0], and then attacks the first GameObject in the Listarray (which is the closest one).

I am running into an issue where one the first enemy in the ListArray is killed, I cannot have the player unit find the next enemy in the list. The enemy is killed through a Destroy(gameObject) script attached as a component to the enemy. I have attempted the following method to get the next closest enemy, though it does not work. Units simply go idle once the first enemy is killed.

 // Update is called once per frame
 void Update () 
 {
     if (updateEnemyList.Count > 0 && updateEnemyList [0] == null)
     Attack (updateEnemyList);

 }

Does anyone have a better way to determine if updateEnemyList [0] has been destroyed by other scripts?

Thanks.

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

Answer by koblavi · Jun 03, 2015 at 02:04 AM

 if (updateEnemyList.Count > 0 && updateEnemyList [0] == null)

The above check will always return false if it's done in the same update loop cycle (Not necessarily the same script) as the element is destroyed because when an object is destroyed, its reference is not to null immediately. Rather it is done just before rendering the corresponding frame.

Here is a snippet to test out the behaviour:

 void Start () {    
         StartCoroutine(CreateAndDestroy());
     }
     
     IEnumerator CreateAndDestroy(){
         //create
         print("Creating Clone");
         clone = Instantiate<Transform>(obj);
         yield return new WaitForSeconds(2);
         //destroy
         print("Destroying Clone");
         Destroy(clone.gameObject);
         //Cheking immediately for null (should return false)
         print("Checking null");
         print(clone==null);
         
         yield return null;
         //wait till next frame and check again
         print("Checking null on next frame (should return true)");
         print(clone==null);
     }


One possible solution will be to delay your null check till the next frame.

Another solution (which I personally think could be simpler if I think through a bit more) is detailed below:

  • Create a custom behaviour which extends the MonoBehavior class and add a custom Destroy method and an isDestroyed field which is set immediately after Destroy executes.

  • Let all your subsequent behaviors extend that class and call the custom destroy method to destroy other Behaviors that extend custom Bevahior.

  • check IsDestroyed property (instead of a null check) to query if an object has been destroyed.

Code snippets for classes are below:

Custom base behavior classs:

 public class MyBehavior : MonoBehaviour
 {
     // Property to check after calling Destroy
     private bool _isDestroyed = false;
     public bool IsDestroyed{ get {
             return _isDestroyed;
         } 
     }
     
     //custom Destroy
     public static void Destroy(MyBehavior obj, bool destoryGameObject = false){
     
         if(destoryGameObject){
             Destroy(obj.gameObject);
         }
         else{
             Destroy(obj);
         }
         obj._isDestroyed = true;
     }
 }

Object that extends custom Behaviour:

 public class DestroyableObject : MyBehavior {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

Instantiating and Destroying our Object (Similar to previous example but replaces null check with isDestroyed() ):

 IEnumerator CreateAndDestroy(){
         //create
         print("Creating Clone");
         clone = Instantiate<DestroyableObject>(obj);
         yield return new WaitForSeconds(2);
         //destroy
         print("Destroying Clone");
         Destroy(clone,true);
         //Cheking immediately for IsDestroyed (should return true)
         print("Checking IsDestroyed");
         print(clone.IsDestroyed);
         
         yield return null;
         //wait till next frame and check again
         print("Checking null on next frame");
         print(clone==null);
     }
     

Hope this helps

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 Ranth · Jun 03, 2015 at 03:48 PM 0
Share

Wow thank you. To be honest, I am new to Unity and would struggle implmenting this well. Right before you posted this I found an alternative, but somewhat similiar method. Ins$$anonymous$$d of destroying enemies when they died, I turned them off. Then in my player script I had functions to check for enemies in the list that had been turned off and removed them from the ArrayList.

However, I thank you for all of the hard work you put into helping me. I really appreciate it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroying 2D Array of Instantated Objects 2 Answers

Why would a GameObject delete itself. 0 Answers

Can anyone help me with a system that helps me to access data inside of data? 2 Answers

Can you destroy an object using GetInstanceID? 0 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