Page 1 of 1

Button onclick save

Posted: Tue Feb 04, 2025 4:27 pm
by lockhighjumpingvenus
Hi, Sorry to need to contact again so soon. However, I am having an issue that I cant find anything in the docs or forums relate to.

I am instantiating a GO at some point during runtime, it has a button attached. The buttons onclick function is determined when it is instantiated. Should I be able to save what this onclick does. I have selected m_OnClick on the prefab ES3, but that doesnt seem to save what the onclick does?

The entire code is basically

Code: Select all

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject newButton = Instantiate(but, spawnInfo.transform.position, Quaternion.identity, spawnInfo.transform.parent);
            newButton.GetComponent<Button>().onClick.AddListener(() => { Debug.Log("Button press"); });
        }
    }

Re: Button onclick save

Posted: Tue Feb 04, 2025 4:36 pm
by Joel
Hi there,

If you're using ES3Prefabs, you shouldn't mark the Button Component to be saved at runtime as this won't need saving as it's part of the prefab (button events can't be saved at runtime but can be instantiated as part of a prefab).

All the best,
Joel

Re: Button onclick save

Posted: Tue Feb 04, 2025 4:53 pm
by lockhighjumpingvenus
Really sorry, but what do you mean by " but can be instantiated as part of a prefab"

The instantiated GO uses a prefab, and the onclick is assigned when it is created. The GO is saved, but the onclick doesnt work when the GO loads back.

Re: Button onclick save

Posted: Wed Feb 05, 2025 7:28 am
by Joel
lockhighjumpingvenus wrote: Tue Feb 04, 2025 4:53 pm Really sorry, but what do you mean by " but can be instantiated as part of a prefab"

The instantiated GO uses a prefab, and the onclick is assigned when it is created. The GO is saved, but the onclick doesnt work when the GO loads back.
In that case if the OnClick event is set outside of the prefab then you would need to manually assign it back after loading as events can't be saved at runtime.

All the best,
Joel