Button component inside the prefab cannot set to interactable=false

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()

    // 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();
            if (i == 5) 
                Debug.Log("Im here");
                newObject.GetComponentInChildren<Button>().interactable = false;

Console Output


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.