How do I add images from the hierarchy view into the inspector? (based on prefabs)

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)
8254560--1080336--upload_2022-7-5_17-21-0.png
These are the images I am dragging into the array
8254560--1080342--upload_2022-7-5_17-26-3.png
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

What exactly doesn´t work? Did you set the “Heart” tag to you prefab in the editor, so is your arrays (heartobj) filled or is it empty ?

Are that gameobjects active in the scene ? With “FindGameObjectsWithTag” you will not find/get inactive gameobjects

The UI Image objects within the canvas have the Heart tag. The canvas itself is not tagged.
8254620--1080375--upload_2022-7-5_18-3-38.png
The heartobj array appears to be empty
8254620--1080384--upload_2022-7-5_18-7-39.png
8254620--1080381--upload_2022-7-5_18-7-21.png

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

I believe the object is active.
8254686--1080387--upload_2022-7-5_18-45-50.png
The debug properly recognises five objects in the array

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 ? :eyes:

The Hearts array is empty. the point of the code I posted earlier was to populate the Hearts array.
8254740--1080393--upload_2022-7-5_19-6-22.png

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
8254833--1080411--upload_2022-7-5_19-56-31.png

8254833--1080411--upload_2022-7-5_19-56-31.png

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];

That´s the only thing i have still in mint

This is the health script as it stands

ublic class Health : MonoBehaviour
{
    public int vitality;
    public int HeartCount;

    public Image[] hearts;    
    public Sprite FullHeart;
    public Sprite EmptyHeart;

    private void Start()
    {
        GameObject[] heartobj = GameObject.FindGameObjectsWithTag("Heart");
        //Debug.Log(heartobj.Length);
        for (int i = 0; i < heartobj.Length; i++)
        {
            Debug.Log(heartobj[i].GetComponent<Image>());
            hearts[i] = heartobj[i].GetComponent<Image>();
            //Debug.Log("for loop entered");
            //Debug.Log(hearts[i]);
            //Debug.Log(hearts.Length);
        }
    }
    private void Update()
    {

       

        if (vitality > HeartCount)
        {
            vitality = HeartCount;
        }

        for (int i = 0; i < hearts.Length; i++)
        {
            if(i < vitality)
            {
                hearts[i].sprite = FullHeart;
            }
            else
            {
                hearts[i].sprite = EmptyHeart;
            }

            if (i < HeartCount)
            {
                hearts[i].enabled = true;
            }
            else
            {
                hearts[i].enabled = false;
            }
        }
    }
}

Try this as first part in the Start() method:
hearts = new Image[5];

1 Like

ok, this works for now. Thank you.

Do you think a more elegant solution is possible? If not, that’s fine. Thanks again.

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.

You´re welcome. Glad I could help :slight_smile:

1 Like