Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Triqy · Oct 23, 2019 at 11:10 PM · arraystringfor-loopforeachcompare

Check if specific object exist in list or array, Best Practices?

What would be the best way to check if an object exist in a array or list using a loop.

If the object exist //do something.

if the object does not exist do something else. Thanks

The method below compares string names of the parameter 'effect.name' but is missing something or i do not fully understanding the check logic or iteration process.

     public void CheckForStatusEffectInPool(Item effect, Transform recipient)
     {
         // Debug.Log(effect.name);
 
         statusEffectPool = statusEffectPoolParent.GetComponentsInChildren<Transform>();
 
         for (int i = 0; i < statusEffectPool.Length; i++)
         {
 
 
             if (effect.name == statusEffectPool[i].name)
             {
                 Debug.Log("Is equal: " + statusEffectPool[i]);
                 statusEffectPool[i].transform.parent = recipient.root;
                 statusEffectPool[i].transform.Find("StatusEffect").gameObject.SetActive(true);
                 break;
 
             }
 
             Debug.Log("This should not fire in the console because of the break;");
 
 
 
 
         }
 
 
     }


the break; at the end appears to not escape the loop and neither does return; when replacing the break;.

Below is the image read out of the console when the method is ran. The method is ran only once with a trigger.

alt text

this is a shot of the inspector for the Status Effect pool array when GetComponentsInChildren() is called in the method.

alt text

capture.png (15.8 kB)
capture1.png (27.1 kB)
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
0
Best Answer

Answer by Triqy · Oct 23, 2019 at 11:45 PM

This is the answer i was looking for. I got rid of the unity array and implemented a generic LIST array and then ran a check with a List.Contain(); method . The code is below.

     public void CheckForStatusEffectInPool(Item effect, Transform recipient)
     {
         // Debug.Log(effect.name);
 
 
         for (int i = 0; i < statusEffectPool.Count; i++)
         {
 
             if (statusEffectPool[i].name.Contains(effect.name))
             {
                 Debug.Log("equal");
                 return;
             }
             else
             {
                 Debug.Log("not equal");
 
             }
 
         }
 
Comment
Add comment · 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
0

Answer by ChuckIes · Oct 23, 2019 at 11:18 PM

Your loop is running twice: the first time it's looking at StatusEffectPool, and since it doesn't match the effect name, it skips your if block and hits the debug message. Then the second time it's looking at WeakHealthDrain and it triggers the if block. You can verify this by switching the order of your elements so WeakHealthDrain is inspected first and the loop breaks before it inspects StatusEffectPool.


If you want to execute code when the for loop fails to find a match, you can use a return instead of a break and place the code after the end of the for loop.

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 Triqy · Oct 23, 2019 at 11:41 PM 0
Share

Thank you for your comment but i tried the return; and is still didnt break out the loop. I also check to see if the trigger was some how calling it twice and it was not.

I found the answer i was looking for by getting rid of the standard array and implementing a LIST variable. I then iterate through my transform and checked with List.Contains() method; with return; at the end. Check out the reply below from me for the full explanation

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

131 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 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 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 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 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 avatar image

Related Questions

Weird issue with List and for loop. 1 Answer

Copy values between two classes in two lists. 1 Answer

Splitting String Into Array 2 Answers

Change part of a string [Solved] 3 Answers

How do I find a String in an Array? 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