Change image visibility of newly instantiated prefab?

In my prefab, there are three stars to change visibility. They are tagged by ‘image1’, ‘image2’ & ‘image3’.
The prefab is attached to the game object called ‘levelObject’. When I’m instantiating, create an instance called ‘newObject’ and I need to change the image visibility inside the for-loop. Hos should I do this?

GameObject.FindWithTag(“image1”) doesn’t work. It can be used for entire prefabs. But not for a specific prefab.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PopulateGrid : MonoBehaviour
{
    public GameObject levelObject;

    Save save = new Save();

    private static bool isThisFirstTime = false;

    void Start()
    {
        if (!isThisFirstTime)
        {
            Debug.Log("First time");
            save.firstTimeInitialization();
            isThisFirstTime = true;
        }
        Populate(save.Loaddata());
    }

    void Update()
    {
        
    }

    void Populate(LevelObject[] levelArray)
    {
        GameObject newObject;
        for (int i=0; i< levelArray.Length; i++)
        {
            newObject = (GameObject)Instantiate(levelObject, transform);
            newObject.GetComponentInChildren<Text>().text = levelArray*.levelId;*

if (i <= levelArray.Length)
{
if (levelArray*.LevelPass == “1”)*
{
newObject.GetComponentInChildren().interactable = true;
if (levelArray*.LevelStars == “0”)*
{
newObject = GameObject.FindWithTag(“Respawn”);
Debug.Log(“no star to play”);
}
else if (levelArray*.LevelStars == “1”)*
{
Debug.Log(“1 star to play”);
}
else if (levelArray*.LevelStars == “2”)*
{
Debug.Log(“2 star to play”);
}

else
{
Debug.Log(“3 star to play”);
}
}
}
else
{
Debug.Log(“no star to play”);
}
}
}
}

Hi! You can use the newobject variable that contains a pointer to the new object you have created (cloned from the prefab you have). In each turn of the loop, the newobject variable is changing its value by the new created object, so you only have to call in each turn the components that you want to change the visibility.

Example:

GameObject[] childImages = newobject.GetComponentsInChildren<Image>();

foreach(GameObject instantImage in childImages)
{
	if(instantImage.tag == "image1")
		instantImage.SetActive(true);
	else
		instantImage.SetActive(false);
}

So the function Populate it could be for example like this:

void Populate(LevelObject[] levelArray)
        {
            GameObject newObject;
            for (int i = 0; i < levelArray.Length; i++)
            {
                newObject = (GameObject)Instantiate(levelObject, transform);
                newObject.GetComponentInChildren<Text>().text = levelArray*.levelId;*

GameObject[] childImages = newobject.GetComponentInChildren();
if (i <= levelArray.Length)
{
if (levelArray*.LevelPass == “1”)*
{
newObject.GetComponentInChildren().interactable = true;
if (levelArray*.LevelStars == “0”)*
{
foreach (GameObject instantImage in childImages)
{
instantImage.SetActive(false);
}
}
else if (levelArray*.LevelStars == “1”)*
{
foreach (GameObject instantImage in childImages)
{
if (instantImage.tag == “image1”)
instantImage.SetActive(true);
else
instantImage.SetActive(false);
}
}
else if (levelArray*.LevelStars == “2”)*
{
foreach (GameObject instantImage in childImages)
{
if (instantImage.tag == “image2”)
instantImage.SetActive(true);
else
instantImage.SetActive(false);
}
}

else
{
foreach (GameObject instantImage in childImages)
{
if (instantImage.tag == “image3”)
instantImage.SetActive(true);
else
instantImage.SetActive(false);
}
}
}
}
else
{
Debug.Log(“no star to play”);
}
}
It is an example based on your function, the ideal would be to reduce the number of times you do the loop to check which image you have to activate.