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 Odie1994 · Jul 14, 2018 at 10:09 AM · turn-basedstrategy

Detect the same variable from multiple scripts

I am making a turn-based strategy game like Civilization. The solution for the turns I found is that I have a 'turnDone' variable on all my units. It is false by default, but after a step it turns true. I also have a script that is supposed to access this value from all the Units that are currently in the game.

 void Update(){
 
 GameObject[] playerUnits = GameObject.FindGameObjectsWithTag ("Player");
         foreach (GameObject playerunit in playerUnits) {
             if( playerunit.GetComponent<Unit>().turnDone){
                 playerunit.GetComponent<Unit>().turnDone = false;
             }
         }
 }

My problem is that it looks at all the scripts individually i.e. if one unit's turnDone is true, this script will turn all the units' turnDone false. Is there a solution to access all the scripts' turnDone values collectively so that it would only turn them false if all the units' turnDone is true?

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 $$anonymous$$ · Jul 14, 2018 at 12:24 PM

Would this work for you?

 int counter;

 for (int i = 0; i < playerUnits.lentgth; i++) {
   if (playerUnits[i].getComponent<Unit>).turnDone) {
       counter++;
    }
 }
 
 if (counter == playerUnits.length) {
   // Run your code
 }
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 Odie1994 · Jul 14, 2018 at 03:12 PM 0
Share

Thank you very much mate! I would never have thought I should use a for loop here. It is working fine! :)

avatar image
0

Answer by JVene · Jul 14, 2018 at 02:07 PM

This can do it:

 bool isok = true;
 
 foreach( GameObject playerunit in playerUnits ) 
  {
    if (playerUnits[i].getComponent<Unit>).turnDone == false ) isok = false;
  }
    if ( isok == true ) {...do your thing....}
 
  

Also, you could break out of the "isok" phase with a return, if that's appropriate, which is a quick exit - because any that aren't done should just quit.

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

87 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

Related Questions

how do you create a grid to move your character within? one square a turn... 0 Answers

Networking and UI Buttons 1 Answer

When moving a character on a grid, how do I animate it? 1 Answer

Looking for pointers or examples of top down turnbased game 0 Answers

Square Based Movement 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