• 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 loserman778 · Feb 14, 2015 at 07:13 PM · c#listdestroy

Destroy first game object in a List of gameObjects once it gets to a certain size

Hello everyone, I'm trying to destroy the first gameobject in a list of gameobjects once it hits a certain size. I cannot for the life of me figure out how to return the game object to destroy. This is what i have so far:

             if(floors.Count >= 9)
             {
                 Destroy(METHOD);
                 floors.RemoveAt(0);
             }

floors is the name of the list. I'm trying to destroy the first gameobject in that list, but i dont know what method to use. I've tried the 'First()' method, but apparently that doesnt work in unity. Also i looked over the MSDN documentation with no luck. If someone could help me out, i would greatly appreciate it. 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
Best Answer

Answer by mattyman174 · Feb 14, 2015 at 10:33 PM

 if(floors.Count >= 9)
 {
   Destroy(floors[0]);
 }

Is that what you wanted?

Comment
Add comment · Show 4 · 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 loserman778 · Feb 14, 2015 at 10:49 PM 0
Share

floors is a List type. Does 'floors[0]' work for lists? I was under the impression that it was just for arrays.

avatar image Phantomized · Feb 14, 2015 at 11:05 PM 1
Share

It does work. It is a global way of getting indexes from collection types. mattyman174's answer does work. however, keep in $$anonymous$$d that since you are only destroying the game object the list references, the list won't resize and floors[0] will simply be null. Consider using floors.RemoveAt(0) afterwards to avoid that.

avatar image loserman778 · Feb 14, 2015 at 11:15 PM 0
Share

Fantastic! Thank you so much for your help!

avatar image mattyman174 · Feb 14, 2015 at 11:38 PM 0
Share

Yes as Phantomized said it is good practice to clean up after yourself if that's what you intended, leaving an index null could lead to undesired behavior.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Add and destroy instantiated gameobject in linkedlist C# 1 Answer

C# override a destroyed GameObject in a List 1 Answer


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