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
1
Question by ifaris7 · Nov 13, 2011 at 03:01 PM · gameobjectslists

List Of GameObjects , Deleting these Objects !

i add objects in Run Time to a -> List();


fruits.Add((GameObject)Instantiate(fruit,transform.position, Quaternion.identity));

OnTriggerEnter() i destroy the Collider GameObject, which is a GameObject inside that list

 void OnTriggerEnter(Collider col)
 {
         Destroy(col.gameObject);
 }


Now my Question is, once i destroy it, will it also be removed from the List, Or i have to manually remove it from the List by accessing it...

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Nov 13, 2011 at 03:06 PM

The list will remain the same length- the destroyed gameObject will be replaced with a null reference. So, neither? I would recommend removing it manually before it gets destroyed, though- that would reduce confusion.

Comment
Add comment · Show 3 · 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 ifaris7 · Nov 13, 2011 at 03:09 PM 0
Share

So if i say this fruits.Remove(col.gameObject);

will it be destroyed :D ?

or i have to do this

fruits.Remove(col.gameObject); Destroy(col.gameObject);

??

Thanks for helping.

avatar image syclamoth · Nov 13, 2011 at 03:11 PM 0
Share

You have to both remove it from the list, and also destroy it. They are two separate operations which do not directly affect one another, but you can only do them in one order (otherwise the object won't exist, so you won't be able to remove it from the list).

avatar image ifaris7 · Nov 13, 2011 at 03:13 PM 0
Share

Oh thank you Sir ;)

avatar image
1

Answer by NDLeo · Nov 01, 2014 at 09:19 PM

Its easier way to do it. Destroy all object in list and then use Delegate to clear all null object in list

 SomeList.RemoveAll (delegate (GameObject o) { return o == null; });
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 Bunny83 · Nov 01, 2014 at 10:46 PM 0
Share

Well, actually it's not easier since it's way longer than SomeList.Remove(item);. However if you can Destroy multiple objects at once, it would be more efficient since RemoveAll iterates through the list only once.

Here's a shorter version:

 SomeList.RemoveAll((o)=>o == null);

Read about lambdas here

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

Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers

Why does the script don't apply to the other duplicated player 0 Answers

How to find all similar elements in a list? 1 Answer

How do i activate a function attached to a listed GameObject from the perspective of itself? 1 Answer

In a group of gameobjects, how to have only one object active at a time while disabling the others? 2 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