Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 JayFitz91 · Feb 20, 2015 at 04:52 PM · gameobjectdatabase

adding GameObjects to a list

I'm trying to add unique GameObjects to a list, for example, each time I collide with an object, I add it to the list, but if I collide with that same object, I do not want to add it to the list again. I have the following:

addEnemy.cs

 if (col.gameObject.tag == "NPC")
 {
     enemyDatabase.addEnemy(col.gameObject);
 }

enemyDatabase.cs

 public List<GameObject> enemies = new List<GameObject>();
 
 
     public void addEnemy(GameObject currentEnemy)
     {
         
         Debug.Log("outside");
         foreach (GameObject enemy in enemies)
         {
             Debug.Log("checking");
             if (currentEnemy.name == enemy.name)
             {
                 Debug.Log("Already in database");
             }
             else
             {
                 enemies.Add(currentEnemy);
             }
 
         }
     }

if i remove the foreach and just add to the database, it works, only the same enemy is added to the database multiple times, I'm assuming the foreach does not run as there are no GameObjects inside of it at the beginning.

Does anyone know a way I can get around this?

Comment
Add comment · Show 5
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 Sospitas · Feb 20, 2015 at 05:00 PM 1
Share

You could just try adding a quick check before the foreach to check that there are objects within the list e.g.:

 if(enemies.Count == 0)
 {
     enemies.Add(currentEnemy);
     return;
 }

That should fix it. There might be a more elegant solution that somebody posts though :)

EDIT: Although, if you have multiple copies of your enemies, that doesn't look like it would work. Once you collide with one enemy type/name, all enemies with that name would have no effect anymore.

@Neamtzu also has a more elegant solution which will remove the need for my fix. So mark their solution as the answer over me. It's just cleaner code :P

avatar image Ben-Stoneman ♦♦ · Feb 20, 2015 at 05:01 PM 0
Share

How many different types of enemies do you have?

avatar image JayFitz91 · Feb 20, 2015 at 05:04 PM 0
Share

@Sospitas sometimes the most simple things go over my head :P That works great, thanks, ill select that as the answer if you convert it to a question :)

avatar image JayFitz91 · Feb 20, 2015 at 05:07 PM 0
Share

@Sospitas Yeah I just got that error there but @Neamtzu solution works perfectly with no errors, convert to answer and ill select. Thanks for the help guys

avatar image Apoorv012 · Dec 03, 2021 at 05:52 PM 0
Share

Can we destroy the complete list at once using destroy(enemies); or do we need to use foreach (GameObject enemy in enemies){destroy(enemy)}; ?

,Can we destroy the complete list at once or do we need to use foreach (...) for every gameObject in that list?

1 Reply

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

Answer by Neamtzu · Feb 20, 2015 at 05:03 PM

Try this:

void addEnemy(GameObject currentEnemy) {

     if(!enemies.Contains(currentEnemy))

       enemies.Add(currentEnemy);

}

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 JayFitz91 · Feb 20, 2015 at 05:07 PM 0
Share

Works great, thanks for the assistance

avatar image Necrohunter · Apr 12, 2020 at 09:35 AM 0
Share

oh jeez i totally forgott about .contains.. i used a for loop to check for every enemy in the list and when removing and adding them again i got a strange out of range error, even thought the list is 0 and i just wanted to add the enemy again after he was removed.

anyway, thanks.

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

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't retrieve information from GameObject 2 Answers

store a gameobject ? 2 Answers

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

Callback function cannot change game object status,callback function cannot change gameobject status 0 Answers

Help on making a Database. 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