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 /
This question was closed Nov 05, 2019 at 05:20 PM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by JNetRocks1316 · Jan 31, 2014 at 06:40 PM · getcomponentforeach

How to use GetComponent in a foreach loop?

I have a list of targets as Transforms and I basically just want to iterate through and do damage to each one. Health is in a script on each game object.

I have the code set up as such:

         foreach(Transform target in attackList){
             Target targetScript = target.gameObject.GetComponent<Target>();
             targetScript.health -= 100;
         }

However, I'm getting the error: "foreach statement cannot operate on variables of type UnityEngine.GameObject' because it does not contain a definition for GetEnumerator' or is not accessible".

I gather I can't use the target.gameObject.GetComponent because gameObject isn't "Enumerator"able(word?). Having trouble figuring out how to get the component without accessing the game object though.

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

3 Replies

  • Sort: 
avatar image
2

Answer by poncho · Jan 31, 2014 at 07:29 PM

if its a list use a for, if its an array you can use the foreach, try

 for(int i =0;i< attackList.Count; i++)
 {
  attackList[i].GetComponent<Target>().healt -= 100;
 }
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
2

Answer by Bunny83 · Nov 05, 2019 at 05:19 PM

Since the question got already bumped I'll post an actual answer even the OP hasn't been online during the last 5 years...


The error clearly states:

The foreach statement cannot operate on variables of type UnityEngine.GameObject


this clearly shows that "attackList" is not an array or generic List<Transform> as the OP said but that it is just a GameObject variable. Of course the GameObject class doesn't have a GetEnumerator method which is required by the foreach pattern matching that the compiler performs. Actually looking at the provided line numbers of the errors would have helped. The error clearly is not related to the GetComponent line

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 Petrit24 · Nov 05, 2019 at 02:52 PM

You could use a foreach loop like so:

 foreach (GameObject obj in aList)
         {
 
             Type variableName = obj.GetComponent<Type>();
 
         }
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

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Definition problem 1 Answer

Looping through materials on a mesh with foreach() in Start() is causing lasting FPS drop? 1 Answer

Accessing children of a gameObject by name 3 Answers

how do I change the properties of a material that are on the children of an object on script? 1 Answer

Get Component or Check if Null SoftLocks Game (C#) 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