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”);
}
}
}
}