I have a prefab with a button component and other three images like below.
Now I instantiating those buttons using a script. But the interactable function doesn’t work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PopulateGrid : MonoBehaviour
{
public GameObject levelObject;
public Text level_index;
public Image star1, star2, star3;
public int noToCreate;
// Start is called before the first frame update
void Start()
{
Populate();
}
// Update is called once per frame
void Update()
{
}
void Populate()
{
for (int i=1; i< noToCreate; i++)
{
GameObject newObject = (GameObject)Instantiate(levelObject, transform);
newObject.GetComponentInChildren<Text>().text = i.ToString();
GameObject.FindGameObjectWithTag("image1").SetActive(false);
GameObject.FindGameObjectWithTag("image2").SetActive(false);
if (i == 5)
{
Debug.Log("Im here");
Debug.Log(newObject.GetComponentInChildren<Button>().interactable);
newObject.GetComponentInChildren<Button>().interactable = false;
}
}
}
}
Console Output
What is the wrong I do here?