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 ElektroKnight · Sep 03, 2017 at 04:01 PM · arrayforeachfindobjectsoftype

How to get array of objects of type "Script" and with variable with value "X"?

I used:

 foreach (EnemyHealth botCount in hazardCount) {

         if (hazardCount.myTeam == GetComponent<BaseDomain>().myTeam) {

             if (botCount.GetComponents<EnemyHealth>().Length >= hazardMax) {
                 StopCoroutine(spawnWaveCoroutine);
             }

             a = botCount.GetComponents<EnemyHealth>().Length;
         }
     }

but "a" returns only one of the array, I need all of objects with script "EnemyHealth" and with team variable equals the respective base team.

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
Best Answer

Answer by ooblii · Sep 03, 2017 at 04:53 PM

Are you just trying to count something, and stop spawning once the loop has reached that number, or are you going to work with the EnemyHealth class?

"a" is only ever going to be an array of however many EnemyHealth scripts you have on the last botCount which satisfies the first condition in your foreach loop.

You could try using FindObjectsOfType. But the way you have it set up, it would probably return elements on both teams.

Why not before you run the loop, declare a List of < EnemyHealth> and when your condition is satisfied, call:

 enemyHealthList.Add(botCount);

If you're simply trying to stop spawning once you've reached hazardMax, a for loop is probably better suited than a foreach.

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 ElektroKnight · Sep 03, 2017 at 05:19 PM 0
Share

Hi, thanks for the help:

     hazardCount = GameObject.FindObjectsOfType<EnemyHealth>();

     foreach (EnemyHealth botCount in hazardCount) {

         if (botCount.myTeam == GetComponent<BaseDomain>().myTeam) {

             List<EnemyHealth> enemyHealthList = new List<EnemyHealth>();

             enemyHealthList.Add(botCount);

             if (enemyHealthList.Count >= hazard$$anonymous$$ax) {
                 StopCoroutine(spawnWaveCoroutine);
             }

             myHazards = enemyHealthList.ToArray();
         }
     }

Two bots per wave are spawned, myHazards(I removed "a") array is returning only one item again

avatar image ooblii ElektroKnight · Sep 03, 2017 at 05:33 PM 0
Share
  List<EnemyHealth> enemyHealthList = new List<EnemyHealth>();

Should be declared before you start the loop. Right now you're just making a new List every time you satisfy the condition.

 myHazards = enemyHealthList.ToArray();

should also be outside the loop as you're just converting a List with a single item to an Array with a single item, and it's always going to be the same.

Just curious, what explicitly are you trying to accomplish? There is some redundancy in the code that could be worked out. Why do you need a myHazards Array when you already have an enemyHealthList?

avatar image ElektroKnight ooblii · Sep 03, 2017 at 06:00 PM 0
Share

I got it! Thank you very much, myHazards is just for I see what's happening in the inspector. Can you see my game? :P https://youtu.be/rEIk$$anonymous$$bI8w4g

Show more comments

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

77 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

Related Questions

How do I compare the position of my player to each elements position of an array of GameObjects? 0 Answers

Array problems 3 Answers

Definition problem 1 Answer

How do I find a local variables index in a foreach loop? 1 Answer

I have an array of players. How can I get my enemy to go to the closest one? 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