Button component inside the prefab cannot set to interactable=false

I have a prefab with a button component and other three images like below.
173944-screenshot-2021-01-10-060544.png


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

173945-screenshot-2021-01-10-061146.png


What is the wrong I do here?

If by “doesn’t work” you mean “doesn’t show up in console output” you need to turn this:
Debug.Log(newObject.GetComponentInChildren().interactable); newObject.GetComponentInChildren().interactable = false;
into this:
newObject.GetComponentInChildren().interactable = false; Debug.Log(newObject.GetComponentInChildren().interactable);
Because right now you send that value to the console BEFORE switching them off. Feel free to tell me if this fixed your issue.