Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Data persistence between scenes does not work on my input field

Discussion in 'Scripting' started by BloomyFractal, Sep 16, 2021.

  1. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    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:
    Name Persistence Problem 1 of 2.png

    But when I go to the Main scene, it is destroyed:
    Name Persistence Problem 2 of 2.png
    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:
    Name Persistence Script.png
    Could you help me please ?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    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://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    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.
    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:
    DisplayName method.png
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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.
     
  5. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    When I add
    .text
    to my variable, it displays nothing: Name Persistence displays nothing when dot_text added.png
    The script is like this:
    DisplayName method with dot_text.png
    My mind is foggy on this, I don't know what I'm doing :(
     
  6. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    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:

    DataPersistence scene persistence 1 of 2.png

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

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

    Save
    Saving.png

    Load Loading.png

    And I figured out how to display only the highest score ! High Score Only.png

    Don't lose hope people !

    https://play.unity.com/mg/other/build-wvv
     
    Angelcho and RemusPlays like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    That is awesome. You really feel smart nailing stuff like this down. Glad to hear you stuck with it.
     
    BloomyFractal likes this.
  8. Angelcho

    Angelcho

    Joined:
    Sep 30, 2022
    Posts:
    1
  9. DavoodFazelpour

    DavoodFazelpour

    Joined:
    Dec 2, 2022
    Posts:
    1