Multiple Text object and Strings to PlayerPrefs

hello.
I want to transfer multiple text objects into PlayerPrefs.
sample code below but gives an error.
How can I bind text objects and strings together?
```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextChanger : MonoBehaviour {
public string FirstTexts;
public string SecondTexts;

public Text[] Texts;
public string[] firstStrings;
public string[] secondStrings;

void Update () {
    if(PlayerPrefs.GetString("SampleText") == FirstTexts){
        Texts.text = firstStrings; // Says error
    }else if(PlayerPrefs.GetString("SampleText2") == SecondTexts){
        Texts.text = secondStrings; // Says error
    }
       
}

}**
```

You could consider using JsonUtility for storing your data, instead: Unity - Scripting API: JsonUtility

Your code’s current issue is that you’re trying to assign an array of strings to a string variable.

If it’s just a couple of strings, and you want to fill a Text object with the value retrieved from PlayerPrefs, you could use:

void Start() {
    string str = PlayerPrefs.GetString("SampleText");
    Texts[0].text = str;
    str = PlayerPrefs.GetString("SampleText2");
    Texts[1].text = str;
  }

I changed the example so that it runs in Start(). Perhaps you want it to run when something happens, in which case you could move it to its own method. I do not think you want it to execute in the Update loop, every frame.
The code I posted also assigns to a Text object from an index in your array. I’m not sure if that’s what you meant, though, so you could try to explain more if I got that wrong.

using System.Collections;
 
using System.Collections.Generic;
 
using UnityEngine;
 
using UnityEngine.UI;
 
public class TextChanger : MonoBehaviour {

    public Text[] answer, answer2, answer3, answer4;
    void Awake () {
       // Load Selected Texts
       if (PlayerPrefs.HasKey("SampleTexts") == true){
       }
   }

    void Update () {
 
        if(PlayerPrefs.GetString("SampleTexts") == "FirstTexts"){
            //Loaded Texts
            answer.text = "Yes";
            answer2.text = "Good";
            answer3.text = "Beautiful";
            answer4.text = "Excellent";
 
        }else if(PlayerPrefs.GetString("SampleTexts") == "SecondTexts"){
            //Loaded Texts
            answer.text = "No";
            answer2.text = "Bad";
            answer3.text = "Ugly";
            answer4.text = "Sorry";
 
        }
 
    }

The button code that will launch the answers is another script attached to the previous scene
   
   //the answers in the upper script will be called.
    public void FirstTextsButton(){
   PlayerPrefs.SetString("SampleText", "FirstTexts");
   SceneManager.LoadScene("NextScene");//will be clicked on the previous scene.
   }  
// the answers in the upper script will be called.
   public void SecondTextsButton(){
   PlayerPrefs.SetString("SampleText", "SecondTexts");
   SceneManager.LoadScene("NextScene");//Clicked button load new scene texts
   }

I wrote the original script like this. every time I get into scripting and fixing things and it’s getting harder.
(I am interested in unity after the work. I have limited time)
now I want to do this script instead of setting each text object in the script, using multiple arrays to manage questions and answers or other writing objects in the editor window.
I would like to do that as multiple text[ ] and string[ ].
I hope that I can tell you what I mean.

Well, a few ideas come to mind. One is to use JSON, or scriptable objects to store the questions and answers.

Doing some tutorials could really help: https://unity3d.com/learn/beginner-tutorials
and : Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

It’s possible that you could write a separated string into players prefs, split it and apply that to your text objects (technically speaking).

I can only point out, again, that looking up the strings in the Update loop like that is not needed.

Beyond that, I’m not really sure what to suggest for you at this point.

1 Like

You’re using an array of objects like it’s a singular object. In your original example, Texts is an array, so there is no Texts.text member variable. Same with your new example.

You need to brush up on your programming fundamentals about types.

1 Like