Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Mrintoxx · Mar 31, 2020 at 03:14 PM · arraylist

Heart bar problem with list/array

Hi guys, I'm tryi'n to do a life bar with heart and not a slider. For that i've created an transform array to get child and then stack them in a list, and later call it to enable or disable the game object. Seems unity doesn't like the way i've done it, can anyone take a look to explain me my errors :D Thanks :)

 public class Health : MonoBehaviour
 {
     public int health = 3;
     int maxLife;
 
     public Sprite fullHeart;
     public Sprite emptyHeart;
 
     [SerializeField] Transform[] Lifes;
     List<GameObject> childObjects;
 
     // Start is called before the first frame update
     void Start()
     {
 
     Lifes = GetComponentsInChildren<Transform>();
     List<GameObject> childObjects = new List<GameObject>();
     foreach(Transform child in Lifes)
         {
             childObjects.Add(child.gameObject);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
       SetLife();
     }
 
     void SetLife(){
         for (int i = 1; i < Lifes.Length; i++)
         {
             if (i < health)
             {
                childObjects[i].SetActive(true); 
             }else{
                 childObjects[i].SetActive(false);
             }
             
         }
     }
 }
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
0

Answer by The_Three_Vs · Mar 31, 2020 at 03:29 PM

Lists are 0-indexed; you'll need to change your for loop to

 for (int i = 1; i <= Lifes.Length; i++)
 {
     if (i <= health)
     {
         childObjects[i - 1].SetActive(true); 
     }else{
         childObjects[i - 1].SetActive(false);
     }     
 }

If, for example, the length of Lifes is 3 and current health is 2, this new loop will check if 1 is less than or equal to 2, which it is, and set the 1st childObject, which has a list index of 0, to be active. The loop will then check if 2 is less than or equal to 2, which it is, and set the 2nd childObject, which has a list index of 1, to be active. The loop will then check if 3 is less than or equal to 2, which it isn't, and set the 3rd and last childObject, which has a list index of 2, to be inactive. Since i is now 4 and greater than Lifes.Length, the loop won't try to access a nonexistent heart and will terminate.

Hope this 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
avatar image
0

Answer by Mrintoxx · Mar 31, 2020 at 05:44 PM

Done for that, i've understanded my mistake.

but the code you show me, it's the same doing that ? :

 for (int i = **0**; i < Lifes.Length; i++)
         {
         
             if (i <= health)
             {
             childObjects[i].SetActive(true); 
             }else{
             childObjects[i].SetActive(false);
             }


But i think my problem is not here, i've got an error :"object reference not set" in this part of code :

     void Start()
     {
 
     Lifes = GetComponentsInChildren<Transform>();
     List<GameObject> childObjects = new List<GameObject>();
     foreach(Transform child in Lifes)
         {
             childObjects.Add(child.gameObject);
         }
     }

I'm getting the child of the GameObject in an Transform array and then to a gameobject list.

But in the inspector the parent gets inside the array, for this reason the for loop was starting at 1.

alt text

any idea ?


capture.png (14.2 kB)
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 The_Three_Vs · Mar 31, 2020 at 06:51 PM 0
Share

GetComponentInChildren searches all the children, and the gameObject that called the function, for the specified component. You can fix the issue by adding if if statement if(child != transform) when adding elements to childObjects. You can then use childObjects.Count ins$$anonymous$$d of Lifes.Length, since childObjects will have the correct length even if the objects are inactive. If you want to maintain Lifes, you will have to create a temporary array without the parent, then assign it to Lifes. For example:

 Transform[] temp = new Transform[Lifes.Length - 1];
 for(int i = 0; i < Lifes.Length; i++)
 {
     if(Lifes[i] != transform)
     {
         childObjects.Add(Lifes[i].gameObject);
         temp[i - 1] = Lifes[i];
     }
 }
 Lifes = temp;

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

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

Destroying scripts from GameObjects 2 Answers

Is it possible to store one dimension of a 2d array into a 1d array? 0 Answers

I'm doing a quiz game I need help. How can I make an array (called random questions) to exit and return to the same are not repeated. 1 Answer

How to read assets folder via route partially generated with a for-loop? 1 Answer

arrays basic ! why my array size is always changing? 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