#Instantiated Buttons don't work on the first click

1 messages · Page 1 of 1 (latest)

muted echo
#

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

#

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);
            });
        }
    }
}
novel bronze
#

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

muted echo
#

yes I did

#

and also btw on working buttons it works right away even when I click into focus

novel bronze
#

and you've confirmed all of the code works? because if so this might be a question for #📲┃ui-ux

muted echo
#

I suppose, everything does what it should, just the first click doesn't work on those specific buttons..

indigo creek
#

If you make it a gameObject you can still assign the button to that variable and it should fix the problem

#

@muted echo

ebon basalt
#

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

muted echo
muted echo
ebon basalt
#

yeah you could do the addListener stuff at runtime if needed

muted echo
#

and you're saying that because performance right?

#

just to be sure

ebon basalt
#

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

muted echo
#

oh well, I have control over the prefabs I instantiate

muted echo
#

at least all of the things I want to do

ebon basalt
#

it is up to you, just wanted to say