Static string data not persistent between scenes

Hi, I am trying to create a data persistence feature in the game that I am creating, but I am stuck. It seems like that my string variable in my static class is not saving the text from user InputField no matter what.

I created a script that holds the static class and it includes a variable called playerName;
On scene, I created an InputField that will allow the player the input their names.
I made another script that sets the InputField’s text to the static class playerName.


After the player enters the name in the InputField and moves on to another scene, the static class playerName will still be empty.

How can I make this work? Thanks!

What you’ve created here is not a static class but a singleton. Below is what a static class looks like. Notice that it does not inherit from MonoBehaviour. That’s because a static class cannot inherit from a non-static class.

Additionally every member variable and member function has to be static in a static class.

public static class MyStaticClass {
    public static string playerName;

    public static MyMemberMethod()
    {
        // do stuff
    }
}

That said just looking at the code I don’t see what would stop the singleton from functioning. Below is the way that I have written my Awake() method. Is your singleton the child of another object? Because DontDestroyOnLoad() won’t function properly if you’re doing it with just a child object. Thus why I change the parent of the object.

public static MyManager Instance { get; private set; }

private void Awake()
{
    if (Instance != null)
    {
        DestroyImmediate(gameObject);
    }
    else
    {
        Instance = this;
        transform.SetParent(null);
        DontDestroyOnLoad(gameObject);
    }
}

Thank you, Ryiah. I am confused right now. I have been following the Junior Programmer pathway in Unity Learn.
Here is a link to that page.

I basically just followed the steps from this page. I tried to add “static” before playerName and adjusted the rest of the code. But when I tested play, it still didn’t work somehow.

No, it is its own object.

7407812--905690--Untitled.png