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 Kukadoo · Dec 23, 2015 at 09:50 AM · loopboolean

How can I use a foreach loop with booleans linked to many game objects?

I'm making a Turn Based Strategy game, and each player has 3 "ship uses" meaning that they can move three units before it switches to the other player's turn.

There are a few different unit types in this game so I gave each of them different "Move limits" to determine how far each unit can move if a player uses a ship use on it, by clicking it. (MoveLim in the code)

Each unit is a separate game object and therefore has its move limit kept track of individually as the script for tracking moveLim is attatched to each individual game object.

The turn system I created relies on the player turns switching after every few ship uses, so I was trying to make it so that when red player finishes with their turn, blue player's moveLim on all its units is reset to 0. Then, when blue player finishes THEIR turn, it would reset all of red player's moveLim on all of the red units back to 0.

The issue is, although I can keep track of all the ship uses, I cannot find a way to reset the moveLims on multiple objects. I thought maybe foreach loops were the answer so I tried this...

void Update() {

     if (shipUse > 3) 
     {
         if(redTurn)
         {
             redTurn =false;
             shipUse = 0;
             BlueTurn.SetActive (true);
             RedTurn.SetActive (false);
             Debug.Log("Red Turn Is Over");
             GameObject.FindObjectsOfType(blueTeamMembership);
                 foreach(GameObject bluey in BlueTeamScript)
                 moveLim = 0;


         }
         else
         {

             redTurn = true;
             shipUse = 0;
             RedTurn.SetActive (true);
             BlueTurn.SetActive (false);

             GameObject.FindObjectsOfType(redTeamMembership);
             foreach(GameObject redey in RedTeamScript)
                 moveLim = 0;

         }                    
     }
 }

but to no avail... Just this error: Assets/Scripts/ShipUseCounter.cs(42,44): error CS1502: The best overloaded method match for `UnityEngine.Object.FindObjectsOfType(System.Type)' has some invalid arguments

I also have a script that designated red team membership and blue team membership as true, attached to respective objects of each player. These are the bools I'm looking for.

Am I just misunderstanding foreach loops?

The RedTurn and BlueTurn set actives just make images in the scene appear that let the player know whose turn it is.

I've been racking my brain on this for quite some time. I'm not great at C# but I have no choice at this point.

Possibly Important Info: A player clicks a unit and it becomes "selected" from there they use the arrow keys to move it from space to space, each movement from one space to another increases the moveLim of the specific unit, until it hits the cap of moveLim for that unit type.

The game is grid based, looks like this: alt text

screenie.png (162.9 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 wibble82 · Dec 23, 2015 at 10:38 AM

Your use of foreach and FindObjectsOfType is not entirely correct.

 void Update() 
 {
      //I am assuming this check is working
     if (shipUse > 3) 
     {
         //this could probably be done a little neater by storing the current player 'index', and
         //the player states in a 2D array, so we don't need to duplicate all our logic. for now
         //though, that's not your problem so we'll leave it with 2 if statements
         if(redTurn)
         {
             //I suspect this stuff to set the turn, reset ship use, and change the 'active' objects are fine
             redTurn =false;
             shipUse = 0;
             BlueTurn.SetActive (true);
             RedTurn.SetActive (false);
             Debug.Log("Red Turn Is Over");
 
             //now we need to find all objects that contain your 'ship' script. 
             //I'm going to assume you have a type (script) called 'BlueTeamScript' we do:
             
             //first we call FindObjectsOfType, passing in the 'type of BlueTeamScript'.
             //this returns an array of objects, which we have to cast to an array of 'BlueTeamScript[]'
             BlueTeamScript[] blueScripts = (BlueTeamScript[])GameObject.FindObjectsOfType(typeof(BlueTeamScript));
 
             //now we can use a foreach loop to iterate over all the scripts in the array
             foreach(BlueTeamScript blueScript in blueScripts)
             {
                 //assuming the script has a public 'moveLim' property, we can now set it to 0 for
                 //the current entry in the array
                 blueScript.moveLim = 0;
             }
         }
         else
         {
             //blue is similar!
         }                    
     }
 }

A big improvement (or way to avoid bugs) that you only have 1 'ship' script, and store a bool on it that says whether it is a blue team or a red team ship. You need to be looking at minimizing duplicate code!

In general, I'd advice (just for your own sanity!) that you do a bit more reading on c# - maybe grab a book on it or something! My example above should work, but it's really important to get a solid understanding of things like for loops and arrays, and I probably can't do a good enough job of explaining them here. That code should work, but understanding why it works it's really important and will be much better in the long run!

Good luck! :)

-Chris

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 Kukadoo · Dec 23, 2015 at 03:22 PM 0
Share

I suppose I could ditch either the blue or red and just have one or the other and use the false as the equivalent for the other... Idk I just start duplicating to label things to spare my sanity, but it only seems to have made the situation worse. I will definitely be reading up on C#, in the meantime though, I'm on a time constraint. Thank you, I'll see if I can get this working.

avatar image Kukadoo · Dec 23, 2015 at 03:33 PM 0
Share

Okay, yeah, this won't work because of the way I have it set up,

that being that I have a "Scout $$anonymous$$ovement, Fighter $$anonymous$$ovement, and Frigate $$anonymous$$ovement" set of scripts that contain individual "$$anonymous$$oveLim" ints...

but I think it gives me a better understanding of what I have to do to get it working. Thanks again.

Just have to switch a few things around.. and finagle it a bit. Have a happy new year!

avatar image
0

Answer by corn · Dec 23, 2015 at 04:30 PM

You're not using neither FindObjectsOfType nor foreach correctly. I assume blueTeamMembership is a variable, hence the error you encountered, FindObjectsOfType needs to be called with a type. It seems like you're looking for all instances of BlueTeamScript, so the way to do it is :

 BlueTeamScript[] blueTeamMembers =  GameObject.FindObjectsOfType<BlueTeamScript>();

Since the result of FindObjectsOfType is now assigned to blueTeamMembers, you can iterate on that array and do whatever operation you need on all these BlueTeamScript instances :

 foreach(BlueTeamScript bluey in blueTeamMembers)
 {
     bluey.moveLim = 0;
 }

Hope that helps !

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making an animation stop once it finishes (trouble with booleans) 1 Answer

how do i make a loop for a boolean? 2 Answers

Making a bool stay true for a second/while animation is playing 2 Answers

Not all code paths return a value in boolean loop 2 Answers

How can I do a foreach loop for an array of booleans? 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