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 JayFitz91 · Aug 23, 2014 at 10:52 AM · gameobjectarrayforeach

Problem with foreach loop with GameObject array

I have a few enemies in my scene that I want to be active one at a time. As i destroy an enemy, the next one will become active. Here's what I'm working with:

EnemyAI.cs

     public static bool isActive = true;
 
     void Update()
     {
         if(isActive == false)
         {
             GetComponent<EnemyAI>().enabled = false;
         }

     }

In the script above, I have the isActive bool set to true but I want the script to be disabled if it is set to false, here's where my problem is:

EnemyBehaviour.cs

 public int shipCount = 13;

 void Start()
 {

     GameObject[] ships = new GameObject[shipCount];
     
     for(int i = 1; i < shipCount; i++)
     {
         ships[i] = GameObject.FindWithTag ("ship" + i);

         foreach(GameObject ship in ships[i])
         {
             EnemyAI.isActive = false;
         }
     }
 }

I want to find all of my ships with the correct tags and set them to false (except for the first one, that's why I start the loop at 1)

I used a for loop to find the objects and I wanted to then use a foreach loop to iterate through that list and set each ships (not including the first one) bool isActive to false but it throws me this error:

alt text

I'm a bit stumped as to how to work around it so if anyone could help me out, I'd appreciate it

capture.png (21.7 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
1

Answer by ajketan · Aug 23, 2014 at 11:35 AM

 public int shipCount = 13;
  
 void Start()
 {
      GameObject[] ships = new GameObject[shipCount];
  
     for(int i = 1; i < shipCount; i++)
     {
         ships[i] = GameObject.FindWithTag ("ship" + i);
     }
 
     foreach(GameObject ship in ships)
     {
         EnemyAI.isActive = false;
     }
 }
 
 

I think this should work for you.... because for a foreach loop you need a collection of objects...in your case an array 'ships' of GameObject type.

Plus ... I am not aware of your game logic... if you want 13 ships then use

  for(int i = 1; i < shipCount + 1; i++)
 
 
 //instead of
 
 
  for(int i = 1; i < shipCount; i++) 

because according to your logic the for loop will execute only 12 times...

Hope that helped...

And thanks...for the vote up in advance... :P

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 · Aug 23, 2014 at 11:41 AM 0
Share

You misunderstood my question so I'm afraid you don't get an upvote ;)

I want my loop to execute 12 times as I don't want my first ship to be disabled, I only want ships with the index 1 through 12 to be disabled, leaving ship 0 active.

The code above disables all of my ships.

avatar image ajketan · Aug 23, 2014 at 06:50 PM 1
Share

my actual reply is for the foreach loop that you put inside the for loop... it should be out of the for loop... then only it will work... and don't bother the vote up for this ;P

The answer that @Bored$$anonymous$$ormon gave is same as $$anonymous$$e and which is correct...

Cheers...!

avatar image
0

Answer by Kiwasi · Aug 23, 2014 at 11:26 AM

This line

     foreach(GameObject ship in ships[i])

Should be this

     foreach(GameObject ship in ships)
Comment
Add comment · Show 5 · 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 · Aug 23, 2014 at 11:33 AM 0
Share

That returns the following:

 The type or namespace name `ships' could not be found. Are you missing a using directive or an assembly reference?
avatar image Kiwasi · Aug 23, 2014 at 11:40 AM 0
Share

Whoops, my bad. $$anonymous$$ill the [] as well. Guess that's a sign that its to late for me to be answering any more questions.

Have edited my answer to be correct now.

avatar image Kiwasi · Aug 23, 2014 at 11:43 AM 0
Share

You probably also want to consider this line inside the loop. As it stands your loop does nothing to each individual ship.

 ship.GetComponent<EnemyAI>().isActive = false;
avatar image JayFitz91 · Aug 23, 2014 at 12:04 PM 0
Share

Cheers @Bored$$anonymous$$ormon, last question for ya. I'm now getting:

 Static member `EnemyAI.isActive' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d
avatar image Kiwasi · Aug 23, 2014 at 07:48 PM 0
Share

You really don't want isActive to be a static member. Go back to your EnemyAI script and remove the static modifier from the declaration of isActive.

Any variable you intend to use for each ship can't be static.

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

24 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

Related Questions

Order a GameObject Array 1 Answer

How would I find the name of all game objects in a c# List 1 Answer

Populate an array of GameObjects with collision? 1 Answer

HELP Find gameObject With tag in another array 1 Answer

Creating an array of prefabs? 4 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