- Home /
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()
{
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?
You realise you print the value of interactable
BEFORE setting it to false, right?
Yes. But it doesn't update the UI as well Just ignore the print function.
Answer by unity_5UuMRw312fOSgQ · Jan 10, 2021 at 02:39 AM
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.
It's not about displaying on the console. The fifth element is not disabled. That's the case.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Source Gameobject rendered when using prefab 0 Answers
Custom Inspector: Using SeralizedProperty changes the prefab values! 0 Answers
Instantiating prefabs creates a clone, not instance of the prefab 1 Answer
"Unable to instantiate prefab. Prefab may be broken" 7 Answers
prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer