- Home /
Instantiating a button prefab with interactable = false shows the button as active for a brief moment
So I'm instantiating a list of buttons via script for my level select in the main menu.
Based on your saved data and last level you've reached, I disable the buttons that follow (interactable = false).
My (small) problem is that when the buttons are shown, they are shown as active for a brief moment and then become disabled. My code is something like this:
void Start() {
// do some stuff
for( int i =0; i < buttonsCount; i++ ) {
//do some stuff
Button btn = Instantiate( prefab, parent );
if( condition )
btn.interactable = false;
//do some more stuff
}
}
Some more details:
I've also tried the "opposite" way (which is better for my case) and saved the prefab as interactable = false, and enabling it by script, but this results in the buttons showing as disabled for a brief moment and then become active.
I've also tried loading the scene asynchronously but that didn't help.
The ScrollView that holds hold the buttons is not active when loading the scene, and is getting active via script when needed to show.
I'm assuming it's related to the fact that it is inactive, but haven't gotten to test it yet (as of now). Not even sure how to fix it if that is indeed the problem.
Any thoughts?
Answer by TheKnightsofUnity · Aug 10, 2018 at 11:37 AM
Hi @jt7785,
Try running your code in Awake instead of Start. According to the documentation, Awake is called when the script object is initialised and Start may be called in other frame after Awake.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html
All the best,
The Knights of Unity
This was a good idea, thanks. However, it didn't work. The thing is, all the instantiation happens before the buttons are even shown. They are the children of another element, which is inactive at first, then gets active when the player clicks "Level Select". And only then, you can see the problem. So it's not like the script hasn't run by now. (I've mentioned this on the first post)
The issue is not that important to be honest, but I'd love to hear if you have another idea.
Your answer
Follow this Question
Related Questions
Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers
Multiple Prefab Instantiation, Camera, Button and Scroll View Question 1 Answer
How do I make a button not interactable for a set amount of time after pressed? 0 Answers
An Instantiated UI Button doesn't work; clicking does nothing. 1 Answer
Buttons not showing up the first time the menu loads, but showing up on all menu openings after 2 Answers