I have a health script that is set up such that it takes an array of images (I have it set up as hearts, not a bar)
These are the images I am dragging into the array
This works fine by just dragging the objects across. However, this only seems to work when the objects are in the map by default.
I want to set this up for multiplayer where the objects are not in the map by default, so I have made the UI and the player script(that Health is attached to) into prefabs.
I am unsure of how to populate the Hearts array now that they are set up as prefabs.
This is my attempt to solve the problem but it doesn’t work
GameObject[] heartobj = GameObject.FindGameObjectsWithTag("Heart");
for (int i = 0; i < heartobj.Length; i++)
{
hearts[i] = heartobj[i].GetComponent<Image>();
}
Does anybody have any ideas? Does somebody have a better solution?
Thanks
With that Debug statement you don´t get the “content/size” of the array. Try this, to see, if you have any object(s) inside of the array: Debug.Log(heartobj.Length
If the message is “0” your array is empty and there is a problem to find the heart gameobjects.
In the screenshort of your first post it looks like the Canvas is active, is it also active if you start the game/ in game mode?
I´m not sure if you find childobjects with “FindGameObjectsWithTag” if the parent is inactive
yes, everything is active and if your array has a length of five, the images are found correctly, so the loading is fine. But the scene also looks fine. You loaded the red heart images i guess, so what is the problem ?
What this means is that I can’t actually change any of the Heart visuals, It will only display the Hearts as static sprites.
The Health script is mean to allow me to change some of the hearts to empty hearts depending on the vitality value.
This is what happens when I drag and drop the Hearts under the Canvas into the Hearts array in the Health script. I want to set it so that it populates the Hearts array automatically
yes sry, i thought the other array is the problem. I´m not sure what the problem is. code wise everything looks fine. Do you define your array with a length ? public Image[ ] hearts = new Image[5];
So for the array not really, you have to define an array with the length of it, if you don´t fill it directly, so there is no other way.
So that´s totally fine to do it like that.