Storing TextMeshPro InputField text in player prefs on quit, reload on enable

Hello! So I am trying to save all the textmeshpro input fields in my scene’s current text into player prefs when teh application or scene is closed, then reload them when the scene is re enabled, Currently I cant get the text to reload. I have 6 save files so each save file should be able to have different input field text. the planName and devName can be differnt from save 1 to 2 and so on (that isnt textmeshpro and works)

If anyone can help that would be great

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class DetailController : MonoBehaviour
{

    [SerializeField]
    [Header("Plan Details")]
    public GameObject gameTitle;
    public GameObject developer;

    [SerializeField]
    [Header("Other")]
    public GameObject quitDisclaimer;
    public bool discardChanges = false;

    CheckBoxManager checkboxManager;
    PlanController planController;

    private void Start()
    {
        Debug.Log(SavesController.saves);
        Debug.Log(SavesController.save);
    }

    void OnEnable()
    {
        if (PlayerPrefs.GetString("save") == "1")
        {
            gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("planName1");
            developer.GetComponent<Text>().text = PlayerPrefs.GetString("devName1");
        }

        else if (PlayerPrefs.GetString("save") == "2")
        {
            gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("planName2");
            developer.GetComponent<Text>().text = PlayerPrefs.GetString("devName2");
        }
        else if(PlayerPrefs.GetString("save") == "3")
        {
            gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("planName3");
            developer.GetComponent<Text>().text = PlayerPrefs.GetString("devName3");
        }
        else if(PlayerPrefs.GetString("save") == "4")
        {
            gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("planName4");
            developer.GetComponent<Text>().text = PlayerPrefs.GetString("devName4");
        }
        else if(PlayerPrefs.GetString("save") == "5")
        {
            gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("planName5");
            developer.GetComponent<Text>().text = PlayerPrefs.GetString("devName5");
        }
        else if(PlayerPrefs.GetString("save") == "6")
        {
            gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("planName6");
            developer.GetComponent<Text>().text = PlayerPrefs.GetString("devName6");
        }

        LoadData();
    }

    void OnApplicationQuit()
    {
        if (!discardChanges)
        {
            SaveData();
        }
        else
        {
            Application.Quit();
        }
    }

    void SaveData()
    {

        var foundInputFields = FindObjectsOfType<TMP_InputField>();
        foreach (var inputField in foundInputFields)
        {
            var parentName = inputField.transform.parent.name;
            PlayerPrefs.SetString(parentName + inputField.name, inputField.text);
        }

        var foundToggles = FindObjectsOfType<Toggle>();
        foreach (var toggle in foundToggles)
        {
            var parentName = toggle.transform.parent.name;
            PlayerPrefs.SetInt(parentName + toggle.name, toggle.isOn ? 1 : 0);
        }
    }

    void LoadData()
    {
        var foundInputFields = FindObjectsOfType<TMP_InputField>();
        foreach (var inputField in foundInputFields)
        {
            var parentName = inputField.transform.parent.name;
            inputField.text = PlayerPrefs.GetString(parentName + inputField.name);
        }

        var foundToggles = FindObjectsOfType<Toggle>();
        foreach (var toggle in foundToggles)
        {
            var parentName = toggle.transform.parent.name;
            toggle.isOn = PlayerPrefs.GetInt(parentName + toggle.name) > 0;
        }
    }

    public void QuitDisclaimer()
    {
        if (quitDisclaimer.activeInHierarchy == false)
        {
            quitDisclaimer.SetActive(true);
        }
        else
        {
            quitDisclaimer.SetActive(false);
        }
    }

}

One note is, your gameTitle and developer objects ought to be “Text” type, not GameObjects. Then you wouldn’t need to call GetComponent every single time you access them. This will make your code both easier to read and faster to run. (If you do need the GameObject reference for any reason you can always do gameTitle.gameObject, and this is faster than GetComponent<>)

Another note, you don’t need all the copied and pasted code for the different save slots. You can do something like:

string saveSlot = PlayerPrefs.GetString("save");
gameTitle.text = PlayerPrefs.GetString("planName"+saveSlot;

It doesn’t look like the PlayerPrefs for “save” (which is read from above) is ever actually written to, so unless it’s written to elsewhere, none of your gameTitle and developer things will be getting loaded (because PlayerPrefs.GetString(“save”) will return empty every time).

It doesn’t look like the fields in SaveData and LoadData do anything with the different save slots right now. Aside from that though, I don’t see anything obvious that would make those things not work.

Have you tried putting in Debug.Logs in SaveData and LoadData to ensure these are being executed when you expect them to be? And probably one in the inner loops as well to make sure that it’s finding all the objects as expected, etc.

Are you sure the discardChanges contains false?

Also you don’t need this:

        else
        {
            Application.Quit();
        }

Application gets the OnApplicationQuit when the application is already trying to quit anyway.

Save is set in another script.

Discard changes is false by defalt

That does not mean it is false by the time of the end of the session. If you don’t change it, why do you have it? If you can change it somehow, somewhere then test it, throw a Debug.Log and check if it is actually false.