0

I am very new to Unity, and programming languages in general, so forgive my noobiness lol.

I am making a notepad, and I have been trying to figure out how to get multiple notes as well as the title of the note. I have a basic script for saving notes, which worked for one note, but I don't know how to save multiple notes. I even tested making 2 saves and wasn't able to.

Here's the script I had, full disclosure, I wrote half of it following a tutorial, the other half was my attempt at making multiple saves

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

public class SaveControl : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }

public string theText1;
    public string theName1;
    public string theText2;
    public string theName2;
    public GameObject ourNote;
    public GameObject namePlaceHolder;
    public GameObject placeHolder;
    public GameObject customName;
    [SerializeField]
    public int noteSave = 1;


    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 OkayButton()
    {
        if (noteSave == 1)
            {
            string key = "NoteContents" + noteNumber;
            Debug.Log("key is '" + key + "'");
            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);
        }

    }
}

Sorry it's super sloppy, I just started Unity like a week ago. I'm guessing the problem is partly that the noteSave variable isn't global so it just get's reset between scenes.

Any help would be greatly appreciated!

Mashimaro7
  • 43
  • 7

1 Answers1

1

You can use the note name to get the note that way you can save multiple notes.

Here's an example

public void LoadButton() {
   // i assume this is your note's title/name
   string noteName = customName.GetComponent<Text>().text;
   string noteContent = PlayerPrefs.GetString("noteName");

   ourNote.GetComponent<Text>().text = noteContent
}

public void SaveButton() {
   string noteName = customName.GetComponent<Text>().text;
   string noteContent = ourNote.GetComponent<Text>().text;
   PlayerPrefs.SetString(noteName, noteContent);
   PlayerPrefs.Save();
}

As you can see in this case you need the note's name to be set to get the note's context.

SyBom
  • 96
  • 9
  • Oh okay, thank you very much! I think I got most of it figured out, just a quick question, how would I make it create a button when there is a new save with said custom name? – Mashimaro7 Apr 22 '20 at 18:12
  • That will make it a bit harder because now you have to save all the names of the saves and load them then use this https://stackoverflow.com/questions/44386294/adding-button-with-text-dynamically-to-ui-at-runtime-in-unity to add the buttons – SyBom Apr 23 '20 at 08:16
  • Well, I decided to leave such complicated things for a future project haha, but I have still been trying to get the multiple saves working, with multiple preset buttons, but it doesn't work. Here's my code currently, https://pastebin.com/32tKJmri Sorry if I'm just being dumb lol, I even tried making it print the note name and the notecontents and it printed properly, but when I click the load button it's just blank. – Mashimaro7 Apr 24 '20 at 16:34