Button onclick save

Discussion and help for Easy Save 3, The Complete Save Game & Data Serializer System for the Unity Engine
Post Reply
lockhighjumpingvenus
Posts: 22
Joined: Wed Oct 11, 2023 9:52 pm

Button onclick save

Post 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"); });
        }
    }
User avatar
Joel
Moodkie Staff
Posts: 5081
Joined: Wed Nov 07, 2012 10:32 pm

Re: Button onclick save

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 22
Joined: Wed Oct 11, 2023 9:52 pm

Re: Button onclick save

Post 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.
User avatar
Joel
Moodkie Staff
Posts: 5081
Joined: Wed Nov 07, 2012 10:32 pm

Re: Button onclick save

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply