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

Multiple Note Saves

Discussion in 'Scripting' started by Mashimaro7, Apr 26, 2020.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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.

    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.UI;
    5.  
    6.     public class SaveControl : MonoBehaviour
    7.     {
    8.         public GameObject namePanel;
    9.         public GameObject savePanel;
    10.         public GameObject menuPanel;
    11.         public GameObject notePanel;
    12.         public GameObject ourNote;
    13.         public InputField namePlaceHolder;
    14.         public InputField placeHolder;
    15.         public GameObject customName;
    16.         public int noteSaves;
    17.         public string num;
    18.         public string theText;
    19.  
    20.  
    21.         public void LoadButton(string num)
    22.         {
    23.             noteSaves = int.Parse(num);
    24.             string noteName = customName.GetComponent<Text>().text;
    25.             string noteContent = PlayerPrefs.GetString(noteName + num, theText);
    26.             menuPanel.SetActive(false);
    27.             notePanel.SetActive(true);
    28.         }
    29.  
    30.         public void OkayButton()
    31.         {
    32.             string noteName = customName.GetComponent<Text>().text;
    33.             PlayerPrefs.SetString(noteName + noteSaves, theText);
    34.             menuPanel.SetActive(true);
    35.             notePanel.SetActive(false);
    36.             namePanel.SetActive(false);
    37.             savePanel.SetActive(false);
    38.             placeHolder.text = "";
    39.         }
    40.     }
    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.

    Any help would be greatly appreciated.
     
  2. TuckerFlynn

    TuckerFlynn

    Joined:
    May 18, 2015
    Posts:
    10
    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;
     
    Mashimaro7 likes this.
  3. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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.
     
  4. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Can someone help out?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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 https://assetstore.unity.com/packages/tools/playersprefs-editor-and-utilities-26656 to view your playerprefs from the editor to again check values.
     
    Mashimaro7 likes this.
  6. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Thanks for the reply, I'll download that asset!

    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.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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?
     
    Mashimaro7 likes this.
  8. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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.
     
    Last edited: Apr 27, 2020
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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.

    What do you expect to happen with that value?
     
    Mashimaro7 likes this.
  10. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Oh my gosh, I am an idiot XD

    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.
     
  11. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SaveControl : MonoBehaviour
    7. {
    8.     public GameObject namePanel;
    9.     public GameObject savePanel;
    10.     public GameObject menuPanel;
    11.     public GameObject notePanel;
    12.     public GameObject ourNote;
    13.     public InputField namePlaceHolder;
    14.     public InputField placeHolder;
    15.     public GameObject customName;
    16.     public int noteSaves;
    17.     public string num;
    18.     public string theText;
    19.  
    20.  
    21.     public void LoadButton(string num)
    22.     {
    23.         noteSaves = int.Parse(num);
    24.         string noteName = customName.GetComponent<Text>().text;
    25.         string noteContent = PlayerPrefs.GetString(noteSaves.ToString, theText);
    26.         menuPanel.SetActive(false);
    27.         notePanel.SetActive(true);
    28.         placeHolder.text = (noteContent);
    29.     }
    30.  
    31.     public void OkayButton()
    32.     {
    33.         theText = placeHolder.text;
    34.         string noteName = customName.GetComponent<Text>().text;
    35.         PlayerPrefs.SetString(noteSaves.ToString, theText);
    36.         menuPanel.SetActive(true);
    37.         notePanel.SetActive(false);
    38.         namePanel.SetActive(false);
    39.         savePanel.SetActive(false);
    40.         print (noteName);
    41.         print (theText);
    42.         placeHolder.text = "";
    43.     }
    44. }
    45.  
    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
    Code (CSharp):
    1. PlayerPrefs.SetString(noteSaves.ToString, theText);
    saying cannot convert method group to string?
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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?
     
    Mashimaro7 likes this.
  13. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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.
     
  14. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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.
     
    Mashimaro7 likes this.
  15. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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.
     
  16. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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 "".
     
    Mashimaro7 likes this.
  17. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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
     
    matkoniecz likes this.
  18. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I'm glad you were able to figure it out. Talking things through is sometimes all that is needed.
     
    Mashimaro7 likes this.