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 Danisuper · Dec 14, 2014 at 10:04 PM · c#lists

Always adds items to list

Hi,i made a list where i store some gameobjects. The gameobjects in the list are sorted by the distance from the player (if i'm not clear i mean element 0: nearest object to player,element 1:2nd nearest object to player etc.)When the player reaches the element 0 of the list this element is removed from the list and stored in another "trash" list. The problem is when i add the item in the trash list,it adds the element at every frame and it should add it once.

 public List listMete = new List(); //list that stores gameobject to reach
 public List listMeteTrash = new List()//trash list,stores reached gameobjects
       void Update()
       {
         if (transform.position == listMete[0].transform.position)
         {
            for(int i=0;i<1;i++)
            {
              listMeteTrash.Add(listMete[0]);
              listMete.Remove(listMete[0]); //here works correctly
            }
         }
       }

Thanks in advance guys! :D

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
0

Answer by richyrich · Dec 15, 2014 at 12:42 AM

     //for(int i=0;i<1;i++)
     //{
       listMeteTrash.Add(listMete[0]);
       listMete.RemoveAt(0);
     //}

The for loop iterates once, so remove

If after removing the zeroth element, you are then updating the transform position to the new zeroeth element, the problem will persist

Comment
Add comment · Show 2 · 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 Danisuper · Dec 15, 2014 at 03:47 PM 0
Share

Thank you for your answer but it didn't change anything,the code always adds the item to the trash list,the problem is in list$$anonymous$$eteTrash.Add(list$$anonymous$$ete[0]);

avatar image richyrich · Dec 15, 2014 at 07:52 PM 0
Share

There is limited information to go on from what you have provided.

If the player position is set to equal the nearest GameObject, then the remove will naturally occur every update. It would be useful to see the code that decides whether to update the position as that is what is likely responsible for the problem

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Make Lists within a List 0 Answers

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

Making a bubble level (not a game but work tool) 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