How to Create Multiple Custom Saves

Hello, I am trying to replicate a code in a script I made following a tutorial, I thought I could just use what I learned in the previous tutorial, which was a code to save text inputted in a field, to make a code for naming saves from a separate inputfield.

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

public class SaveControl : MonoBehaviour
{

    public string theText;
    public string theName;
    public GameObject ourNote;
    public GameObject namePlaceHolder;
    public GameObject placeHolder;
    public GameObject saveAnim;
    public string customName;

    void Start()
    {
        theText = PlayerPrefs.GetString("NoteContents");
        placeHolder.GetComponent<InputField>().text = theText;
        namePlaceHolder.GetComponent<InputField>().text = theText;
        string customName = theName;
    }

    public void SaveNote()
    {
    theText = ourNote.GetComponent<Text>().text;
    customName = theName.Getcomponent<Text>().text;
    PlayerPrefs.SetString("NoteContents", theText);
    PlayerPrefs.SetString("theName", customName);
        StartCoroutine(SaveTextRoll());
    }

    IEnumerator SaveTextRoll(){

        saveAnim.GetComponent<Animator>().Play("SavedAnim");
        yield return new WaitForSeconds(1);
        saveAnim.GetComponent<Animator>().Play("New State");

    }


}

I keep getting an error at line 28, string does not contain definition for GetComponent. What am I doing wrong?

Also, I imagine the code I’m trying to make here doesn’t work for multiple saves. Is there a line I could insert to make it save a separate save file every time?

Thanks

The error explains the problem pretty well. “theName” is a string. It doesn’t have a Get Component method. Maybe you mean to use namePlaceholder instead, which is a GameObject?

1 Like

Oh geez, I wish these error codes were in English instead of C#! lol, I think I figured it out. Now, I need to remember what I was doing. I also realized after, I typed Getcomponent. :S
Thanks for the help

Sorry to ask two questions in one post, but I still need help on the second bit about making multiple saves, too.

If you want to have different “saves”, in PlayerPrefs, you will need to use different keys when you call PlayerPrefs.SetString(). For example right now you have hardcoded “NoteContents” and “theName” as your keys:

    PlayerPrefs.SetString("NoteContents", theText);
PlayerPrefs.SetString("theName", customName);

Maybe try appending a number to the end of the key, and changing that number for each different piece of saved data you want. E.g.:

PlayerPrefs.SetString("NoteContents" + saveNumber, theText);
PlayerPrefs.SetString("theName" + saveNumber, customName);

This is by no means a complete solution to what you’re looking for, but hopefully this inspires you.

1 Like

Ahh, sorry for my super noobiness. I forgot you can add to strings, thanks for the help