Data persistence between scenes does not work on my input field

Hello, I’ve been trying to store a Text from an input field and pass it to the main game scene, but it returns null.

When I start the game, the Player Name value is there:

But when I go to the Main scene, it is destroyed:


I feel like I must preserve the Player Name value somehow, but I’ve no idea what the process should be.
Here is the name value persistence script:

Could you help me please ?

Nothing special about that! All the same rules apply, regardless of what you are doing.

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7

So, I read through the PLBM (what do these letters stand for anyway ?) page, and I was able to preserve the playerName variable by making it a child of the empty gameObj., which has a DontDestroyOnLoad function on it. However the game displays a “technical” name instead. If I try to follow the guide that you linked, I think my problem is in the “why is it not being set up?” step.
7502846--924490--Name Persistence value is set up but not changed.png
I suppose I need to rearrange my name displaying code, but I don’t know how:
7502846--924497--DisplayName method.png

Be sure to reason about what is the score (numeric) and what is the presentation text.

That name tells you what it is: you are printing an object of type UnityEngine.UI.Text

Do you perhaps instead mean to print the .text field of that object?

But even that seems weird: you probably want to be using the same approach that you use to make "Score: 0" and instead make "Best Score: 0" for your top score.

The way you are going at it above, it is going to read:

Best Score: Score: 0

Which I’m pretty sure is NOT what you want.

When I add .text to my variable, it displays nothing:7505239--925058--Name Persistence displays nothing when dot_text added.png
The script is like this:
7505239--925060--DisplayName method with dot_text.png
My mind is foggy on this, I don’t know what I’m doing :frowning:

I DID IT !

It was a lot of trouble because I would lose motivation from time to time and was also busy IRL, but I completed the Scene Flow and Data Persistence tutorial ! It took me almost 4 months, but I did it damn it !

Data persistence between scenes (1 of 2). This video was particularly useful:

Data persistence between scenes (2 of 2)
7779528--981366--DataPersistence scene persistence 2 of 2.png

I used the JSONUtility functions for data saving and loading. Aka Data persistence between sessions.

Save

Load

And I figured out how to display only the highest score !

Don’t lose hope people !

That is awesome. You really feel smart nailing stuff like this down. Glad to hear you stuck with it.

Hello there

How does it looks SaveData class ?

hi you can use this link. its my repo for this mission
https://github.com/DFazelpour/Data-Persistence-Project