• 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 ayockel90 · Oct 26, 2020 at 06:06 PM · arrays

How to check the same boolean in all child objects?

I have a box of items. Each item has a script with a boolean called isAtStartPos, which is true when the object's transform.position is equal to its start position.

I have another script on the box which needs to know if all of these child objects are at their start positions but can't seem to figure out how to achieve this through code. I have the below function, but it always returns true. How can I get it to return false?

public class Restock : MonoBehaviour {

 Transform[] children;
 GameObject[] allChildren;
 Transform randomObject;
 public AudioSource audio;
 public AudioClip pop;
 public AudioClip kaching;
 public GameObject Player;
 public GameObject restockingGIF;
 public GameObject crosshairs;
 public GameObject radialProgress;
 float resetTime;
 int index;
 bool isFinished;
 bool allChildrenAtStart;
 public float resetSpeed;
 bool allChildrenReset;

 // Start is called before the first frame update
 void Start()
 {
     children = gameObject.GetComponentsInChildren<Transform>();
     allChildren = new GameObject[transform.childCount];
     resetTime = 0.0f;
     allChildrenAtStart = true;
 }

 // Update is called once per frame
 void Update()
 {
     float dist = Vector3.Distance(Player.transform.position, transform.position);

     if(Input.GetKey(KeyCode.E) && (Time.time - resetTime) > resetSpeed && dist <= 4 && Player.GetComponent<PlayerMovement>().shotgunEquipped == false){
         ResetRandomObject();
         crosshairs.SetActive(false);
         restockingGIF.SetActive(true);
     }
     if(Input.GetKeyUp(KeyCode.E) && Player.GetComponent<PlayerMovement>().shotgunEquipped == false){
         crosshairs.SetActive(true);
         restockingGIF.SetActive(false);
     }

 }

 void ResetRandomObject(){
     int i = 0;
     foreach (Transform child in transform){
         allChildren[i] = child.gameObject;
         i += 1;
     }
     int index = Random.Range(0, allChildren.Length);
     GameObject currentChild = allChildren[index];
     if(currentChild.GetComponent<ResetObject>().isAtStartPos == false){
         StartCoroutine(currentChild.GetComponent<ResetObject>().Reset());
         audio.PlayOneShot(pop);
         //finished = true;
         resetTime = Time.time;           
     }
     CheckIfAllReset();
 }

 IEnumerator Finished(){
     audio.PlayOneShot(kaching);
     yield return new WaitForSeconds(1);
     isFinished = false;
 }

 bool CheckIfAllReset(){
     for(int i = 0; i < allChildren.Length; i++){
         if(allChildren[i].GetComponent<ResetObject>().isAtStartPos == false){
             Debug.Log("not all children at start");
             return false;
         }
     }
     Debug.Log("all children at start");
     return 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by rhapen · Oct 26, 2020 at 07:21 PM

I would use little different way to loop. But either way should be ok. The question is maybe how and where do you populate the allChildren?

 bool CheckIfAllReset(){
          foreach(GameObject go in allChidren){
              if(!go.GetComponent<ResetObject>().isAtStartPos){
                  Debug.Log("not all children at start");
                  return false;
              }
          }
          Debug.Log("all children at start");
          return true;
      }

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 ayockel90 · Oct 27, 2020 at 01:49 AM 0
Share

Nope, neither code works. allChildren is populated as below

 GameObject[] allChildren;
 
 void Start()
 {
      allChildren = new GameObject[transform.childCount];
 }
avatar image shahprateek317 ayockel90 · Oct 27, 2020 at 02:22 AM 0
Share

The code in your Start() method only seems to create a new (empty) GameObject array of a certain length. Where do you populate the array with actual Gameobjects?

avatar image ayockel90 shahprateek317 · Oct 27, 2020 at 05:44 AM 0
Share

Sorry I've updated with the full script.

Show more comments
avatar image
0

Answer by RLord321 · Oct 27, 2020 at 02:03 AM

The loop is fine. Please show how you are setting the allChildren array to true...

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 ayockel90 · Oct 27, 2020 at 05:43 AM 0
Share

Sorry, I've updated with the full script.

avatar image
0

Answer by unity_MXGApNcapNfNLg · Oct 27, 2020 at 07:13 AM

Hi, I am pasting my own piece of code. I hope you can understand it.

public void CheckDone() { if (IsAllEggsPlaced()==true) { Debug.Log("Finished"); }

     if (IsAllEggsEmpty() == true)
     {
         Debug.Log("Empty Bro");
     }

 }

 private bool IsAllEggsPlaced()
 {
     for (int i = 0; i < emptyPlaces.Length; ++i)
     {
         if (emptyPlaces[i].GetComponent<CheckObjectFlag>().egg == false)
         {
             return false;
         }
     }

     return true;
 }

 private bool IsAllEggsEmpty()
 {
     for (int i = 0; i < emptyPlaces.Length; ++i)
     {
         if (emptyPlaces[i].GetComponent<CheckObjectFlag>().empty == false)
         {
             return false;
         }
     }

     return true;
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

143 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 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

Using arrays and for loop with GetComponent error 1 Answer

How to select a texture in an inventory? 0 Answers

How to load an inventory array? 1 Answer

need to shorten my code but unsure of how 3 Answers

Change multiple gameobjects colour to create a trail 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