How to Show current save file title in game

If I start a new game with a name “ABC” ABC.sav in the MainMenu Theme,
How do I print “ABC” in the game scene?

I have tried:

   SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
   savingWrapper.GetCurrentSave();
   print(savingWrapper.GetCurrentSave());

in Awake and OnEnable. None of it worked.

I have only one PersistentObjectSpawner that contains savingWrapper in the MainMenu Scene. I have removed all other PersistentObjectSpawners from other Scenes.

What you have will output to the Console, but not the game scene.

If you’re looking to have the current save in a text object in the UI, try this:

using RPG.SceneManagement;
using TMPro;
using UnityEngine;

[RequireComponent(typeof(TextMeshProUGUI))]
public class CurrentSaveFileDisplay : MonoBehaviour
{
    private TextMeshProUGUI text;

    private void Awake()
    {
        text = GetComponent<TextMeshProUGUI>();
    }

    private void OnEnable()
    {
        SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
        text.text = savingWrapper.GetCurrentSave();
    }
}

Put this on a UI Object with a TextMeshProUGUI component.

Thanks Brian! It works. I have changed Awake() to Start(), because I have received some errors at Awake() . Start() has no problem.

That’s surprising… OnEnable should be throwing you an error if you cache the GetComponent in Start() because it’ll be accessing text before it’s initialized.

Of course, you can dispense with the GetComponent altogether by making it a [SerializeField] and assigning it in the inspector.

Privacy & Terms