Save Script Not Working

Hello, I’m trying to make a save script for multiple saves, it’s just a notepad so all it saves is the custom name and the note. I had it working with single saves, so I tried rewriting it to work with multiple saves and just called them “NoteContents1”, “NoteContents2” and so on. But when I click the OkayButton() it doesn’t save anything, I think the variable noteSave just resets to 0?
Here’s my code, I understand it’s very sloppy, but I’m working on limited C# skills here :stuck_out_tongue:

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

public class SaveControl : MonoBehaviour
{

    public string theText1;
    public string theName1;
    public string theText2;
    public string theName2;
    public GameObject ourNote;
    public GameObject namePlaceHolder;
    public GameObject placeHolder;
    public GameObject customName;
    static int noteSave;


    public void LoadButton1()
    {
        {
            SceneManager.LoadScene(1);
            noteSave = 1;
            theText1 = PlayerPrefs.GetString("NoteContents1");
            theName1 = PlayerPrefs.GetString("theName1");
            placeHolder.GetComponent<InputField>().text = theText1;
            namePlaceHolder.GetComponent<InputField>().text = theName1;
            string customName = theName1;
        }
    }
    public void LoadButton2()
    {
        noteSave = 2;
        theText2 = PlayerPrefs.GetString("NoteContents2");
        theName2 = PlayerPrefs.GetString("theName2");
        placeHolder.GetComponent<InputField>().text = theText2;
        namePlaceHolder.GetComponent<InputField>().text = theName2;
        string customName = theName2;
        SceneManager.LoadScene(1);
    }
    public void LoadButton3()
    {
        noteSave = 3;
        theText3 = PlayerPrefs.GetString("NoteContents3");
        theName3 = PlayerPrefs.GetString("theName3");
        placeHolder.GetComponent<InputField>().text = theText2;
        namePlaceHolder.GetComponent<InputField>().text = theName2;
        string customName = theName3;
        SceneManager.LoadScene(1);
    }

    public void OkayButton()
    {
        if (noteSave == 1)
            {
            theText1 = ourNote.GetComponent<Text>().text;
            theName1 = customName.GetComponent<Text>().text;
            PlayerPrefs.SetString("NoteContents1", theText1);
            print(theName1);
            SceneManager.LoadScene(0);
            }
        else if (noteSave == 2)
        {

            theText2 = ourNote.GetComponent<Text>().text;
            theName2 = customName.GetComponent<Text>().text;
            PlayerPrefs.SetString("NoteContents2", theText2);
            print(theName2);
            SceneManager.LoadScene(0);
        }

    }
}

Any help would be appreciated! Thanks!

You probably want to learn how to do loops and iterate over collections and count up from 1 to whatever number you want, otherwise when you have 500 notes you’re gonna have to copy/paste a LOT of code.

For this application there should be one load function that can load any note given a number, and one save function that saves a particular note number with its attendant data. The rough analogy is that the phone company does not write a copy/paste a new program every time it gets a new customer with a new number!

Also, if you expect noteSave to not be zero, you would have to also save it somewhere (such as to PlayerPrefs) and read it back on start, or else connect it as a parameter coming in from the button object.

1 Like

Sorry for my noobiness, how do you save a variable to playerprefs? Would you please be able to give an example with the above noteSave variable? Also, I know what loops are, and how to make a loop, but I’m unsure of how to change the “i” value that is being searched. Say if I click loadbutton for save 2, how do I run a for loop that looks for 2?

You are hard-coding keys like “NoteContents3”. Instead you would produce a temporary string variable this way:

int noteNumber = 1;  // this would be your iterating loop variable

string key = "NoteContents" + noteNumber;

Debug.Log( "key is '" + key + "'");

string contents = PlayerPrefs.GetString( key);

At that stage contents contains whatever was in the “NoteContents1” playerpref string.

1 Like

Cool, I got it now. Thanks for the help!