I instantiate multiple buttons in a layout group at the start of the scene. The first time I click any button it doesn't work, but as soon as I clicked anyone, all of them work again until exit scene. Even if they get deleted and spawned again. This only reoccurs when I enter the scene again. When I drag the button prefab into the scene and assign a function, it works first try, but the spawned ones still have the issue. I haven't been able to get rid of it for an eternity now and I'm not sure what I'm missing. Is there a quirk of Unity's UI sometimes? I'll post the instantiation code in the replies
#Instantiated Buttons don't work on the first click
1 messages · Page 1 of 1 (latest)
Notably, the createBtn thats spawned works on the first try, the others don't.
public class CampaignCardsDisplay : CampaignListDisplay
{
[SerializeField] private Transform cardParent;
[SerializeField] private CampaignListItem campaignCardPrefab;
[SerializeField] private Button createCampaignBtn;
public override void DisplayCampaigns(List<CampaignListItemDto> campaigns)
{
// clear the card parent first
cardParent.DeleteChildren();
// add create campaign btn
Button createBtn = Instantiate(createCampaignBtn, cardParent);
createBtn.onClick.AddListener(() => CampaignManager.Instance.CreateCampaign());
// Add Campaigns
for (int i = 0; i < campaigns.Count; i++)
{
CampaignListItem campaignCard = Instantiate(campaignCardPrefab, cardParent);
campaignCard.SetCampaign(campaigns[i]);
campaignCard.GetComponent<Button>().onClick.AddListener(() =>
{
CampaignManager.Instance.QuickViewCampaign(campaignCard.campaign);
});
}
}
}
just to be certain, you had clicked into the game view to focus that window before attempting to click the buttons, yes? because if not that first click is going to just give the game view focus then your UI would work
yes I did
and also btw on working buttons it works right away even when I click into focus
and you've confirmed all of the code works? because if so this might be a question for #📲┃ui-ux
I suppose, everything does what it should, just the first click doesn't work on those specific buttons..
So instead of the variable being a private button make it a private game object
If you make it a gameObject you can still assign the button to that variable and it should fix the problem
@muted echo
just a little reminder, ever thought of enabling and disabling gameObjects instead of instantiating ui elements?
this would allow you to do everything before runtime, design, position addlisteners and all you need
i know it is not related to your request but just wanted to say
interesting, why would that fix it though?
didn't think about it much before. I guess that might be an option I could try. The addlisteners and stuff is harder before runtime because I'm getting a list from my server during runtime and the listeners need the objects from that list
yeah you could do the addListener stuff at runtime if needed
nothing to do with performance, it is just that you have full controll over the ui element and you can work comfortably in the editor and do whatever you want. at runtimr everythign needs to be done via code
might be even a fix to your current problem if you have everything set up , instead instantiating
oh well, I have control over the prefabs I instantiate
that might be true and I think I'll try it, but I don't understand the point of more control if I can control all of that with code just the same
at least all of the things I want to do
it is up to you, just wanted to say