Hello, I know I’ve asked similar questions before so sorry, but I’ve been trying to figure this out for ages and I just can’t get it working. I’m making a notepad app, and am trying to get multiple saves, in the player prefs named after each save.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveControl : MonoBehaviour
{
public GameObject namePanel;
public GameObject savePanel;
public GameObject menuPanel;
public GameObject notePanel;
public GameObject ourNote;
public InputField namePlaceHolder;
public InputField placeHolder;
public GameObject customName;
public int noteSaves;
public string num;
public string theText;
public void LoadButton(string num)
{
noteSaves = int.Parse(num);
string noteName = customName.GetComponent<Text>().text;
string noteContent = PlayerPrefs.GetString(noteName + num, theText);
menuPanel.SetActive(false);
notePanel.SetActive(true);
}
public void OkayButton()
{
string noteName = customName.GetComponent<Text>().text;
PlayerPrefs.SetString(noteName + noteSaves, theText);
menuPanel.SetActive(true);
notePanel.SetActive(false);
namePanel.SetActive(false);
savePanel.SetActive(false);
placeHolder.text = "";
}
}
This is my code so far, but it doesn’t work.I tried testing it by printing the name and notecontents to the debug log, which it does successfully, but it doesn’t seem to load it. Sorry if I’m not understanding playerprefs, but I just can’t get it working. For the record, it’s all in one scene.
What part doesn’t work? Are you getting errors or just not what you were expecting?
I’m guessing you’re trying to display the string noteContent, if that’s what isn’t working I would say it’s because you’re giving noteContent a value but not actually using that value for anything. If you have a public Text UI element you would set it with something like myTextElement.text = noteContent;
Sorry, I should’ve clarified, it doesn’t work at all, it just loads a blank canvas.
Ahh, I think I see the issue. The placeholder is the name of the input field. theText is the text that I’m saving. I’m not sure how to go about this though, When I try setting the string in the OkayButton(save button) it just says NoteContent doesn’t exist in this conext.
It’s a little difficult for me to tell exactly how your setup is with this code. When you go to save a note, how is the value of “theText” populated? That is the text you’re currently saving. Assuming “theText” is properly assigned a value, the next question is how is noteSaves assigned a value in your setup?
With that all in mind, you should be adding debugs to find out what value things are to make sure they are getting the proper value. And, you can also use something like PlayersPrefs Editor and Utilities | Tools | Unity Asset Store to view your playerprefs from the editor to again check values.
theText is supposed to be set at the OkayButton()
PlayerPrefs.SetString(noteName + noteSaves, theText);
and noteSaves is set to the int num of the load button, I have the load buttons set up to be a different number(1,2,3, etc) and I’m trying to set the PlayerPrefs to be saved under the name the player typed in. So if you clicked LoadButton(1) It would save as noteName1.
theText is a string variable. Where is that string set? The line of code you reference sets a playerpref, not the value of theText. Where is that string value set?
Ahh, my bad, I accidentally removed that line in my adventures of trying to fix this lol. Added theText = placeHolder.text; to the top of the OkayButton() method, but it still doesn’t work. I even added a print(theText) and it prints just fine, but it seems it’s either not saving or not loading properly.
Edit: Sorry, I forgot to try out that asset, I just ran it, it seems to be saving the player prefs perfectly fine, it would appear loading is the issue.
Your loadButton method assigns the text from the playerpref to a string noteContent, which is declared in the method as well. But you never seem to do anything with that value and so once the method ends, the information is lost.
Okay, well, I have it saving and loading with each respective button, but when I close the game, the saves just reset. Sorry to keep asking questions, but do you know what would be causing this? I’m guessing it’s not saving the names of the buttons, just the data inside said buttons, but I’m unsure how to fix it.
Edit: Ah, My bad, it only really fixed the issue of it loading blank while ingame, it still doesn’t save them to independent buttons. I thought I had it working, man, this is a difficult thing to do haha.
Well, I tried setting the name saved in the playerprefs as simply the number for the load button, so I tried having it set the noteSaves variable whenever it loaded, but I ran into two issues, here’s my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveControl : MonoBehaviour
{
public GameObject namePanel;
public GameObject savePanel;
public GameObject menuPanel;
public GameObject notePanel;
public GameObject ourNote;
public InputField namePlaceHolder;
public InputField placeHolder;
public GameObject customName;
public int noteSaves;
public string num;
public string theText;
public void LoadButton(string num)
{
noteSaves = int.Parse(num);
string noteName = customName.GetComponent<Text>().text;
string noteContent = PlayerPrefs.GetString(noteSaves.ToString, theText);
menuPanel.SetActive(false);
notePanel.SetActive(true);
placeHolder.text = (noteContent);
}
public void OkayButton()
{
theText = placeHolder.text;
string noteName = customName.GetComponent<Text>().text;
PlayerPrefs.SetString(noteSaves.ToString, theText);
menuPanel.SetActive(true);
notePanel.SetActive(false);
namePanel.SetActive(false);
savePanel.SetActive(false);
print (noteName);
print (theText);
placeHolder.text = "";
}
}
I realized it has no way of setting the noteSaves variable to the number of the button, how would I grab the string off of the LoadButton that is being pressed and put it as the save name? The second issue is even if I could do that, it’s giving me an error at
ToString is a method, so it requires () at the end of it.
When you click a button to load, isn’t the string num value the value you want? And, honestly, your code is a little odd. You take a string as a parameter, convert that into an int with noteSaves. Then you try and convert the int back into a string. Why the double conversion for the same value?
Okay, well I have it 75% the way lol. It’s saving to separate playerprefs, but it seems the placeHolder.text = “”; part isn’t working out. So it loads properly if I quit and start again, but it isn’t clearing the input field if I say, leave on note and go to another.
I’ve just noticed the redundancy hahaha, it was because originally I had the notesaves variable increase when I saved the game, but I realized that wasn’t working out so well so I attached a string to the loadbutton instead. Like I said, I’ve been trying to figure this out for ages, so many forum posts, reddit posts, stack overflow posts and I’ve only finally gotten close to solving it XD. I’m still 2 weeks in to learning Unity.
I’m not seeing anything that would make placeHolder.text = “”; not run as long as the OkayButton method is called. But I don’t know what inputfield is assigned to placeHolder, so I would say double check that is correctly assigned.
It’s supposed to clear when the okay button is pressed, the okay button saves the text, then closes the input field, then clears the text. But for some reason it’s just not clearing the text. It should be set right because it’s properly saving, I even tried moving the placeHolder.text = “”; before the panel gets disabled, but same result. It’s saving properly, the issue is if I load a note that was previously blank, it will not clear the input field. So say I type Hello into Note 1 and Bye in note 2, i can alternate between the two just fine, but if I load number 3 it will be either hello or Bye, depending on whatever I previously opened.
Sorry, I’m having trouble picturing your scene layout.
So you have multiple notes and you are able to load that note which opens up a new dialog box/panel etc that has an inputfield and this inputfield is populated with what is in the note normally?
Also, when you save the note, you are checking the inputfield and it still shows the text put in at that moment? Could you also add a print after the placeholder.text = “”; part and just print out the value of placeholder.text. This will help verify nothing is changing the value back after you set it to “”.
Oh, I just fixed it right now, the issue was I was clearing the placeHolder.text instead of clearing theText. I don’t understand why that would be the issue because theText = placeHolder.text lol, but it’s fixed now.
Thank you so much for all your help! You have no idea how much I struggled with this haha