- Home /
Saving or get array of string - PlayerPrefs
Hello world,
I would save and get list, using an array (of course), by PlayerPrefs but it doesn't work, or unity doesn't recognize my scirpt, i've a lot af compiler errors.
Please note that I not use the PlayerPrefsX/ArraysPrefs2 - I prefer use my own script cause I prefer improve me to scripting.
Here's my script :
#pragma strict
import System.Collections.Generic;
import System.Collections;
public var ItemList : List<ItemArray>;
public var CheatCodesList : List<CheatCodeArray>;
var CheatCodeArray : String[];
var ItemArray : String[];
function GetItemList () {
var readLine : String = PlayerPrefs.GetString("Items");
ItemArray = readLine.Split("*"[0]);
}
function GetCheatCodeList () {
var readLine : String = PlayerPrefs.GetString("Items");
CheatCodeArray = readLine.Split("*"[0]);
//for(var i : int = 0; i < readArray.Length; i++) {
//ItemList = readArray;
//}
}
public function SaveItemList () {
var temp : String = "";
for(var i : int = 0; i< ItemList.Count; i++) {
if (i != ItemList.Count - 1)
temp += ItemList[i].ToString() + "*";
else
temp += ItemList[i].ToString();
}
PlayerPrefs.SetString("Items", temp);
}
public function SaveCheatCodeList () {
var temp : String = "";
for (var i : int = 0; i < CheatCodesList.Count; i++) {
if (i != ItemList.Count - 1)
temp += CheatCodesList[i].ToString() + "*";
else
temp += CheatCodesList[i].ToString();
}
PlayerPrefs.SetString("Items", temp);
}
Thanks for avcande
It seems your list are not initialized only the reference is declared. What are your errors?
Also, I think in UnityScript, list declaration takes a . :
var list : List.<Type>();
Declare :
var list : List.<Type>;
Initialize :
list = new List.<Type>();
One-Liner :
var list : List.<Type> = new List.<Type>();
note the full-stop (period)
I do it, but Unity doesn't recognize my type in the list :
#pragma strict
import System.Collections.Generic;
import System.Collections;
public var ItemList : List.<ItemArray> = new List.<ItemArray>();
public var CheatCodesList : List.<CheatCodeArray> = new List.<CheatCodeArray>();
public var CheatCodeArray : String[];
public var ItemArray : String[];
function GetItemList () {
var readLine : String = PlayerPrefs.GetString("Items");
ItemArray = readLine.Split("*"[0]);
}
function GetCheatCodeList () {
var readLine : String = PlayerPrefs.GetString("Items");
CheatCodeArray = readLine.Split("*"[0]);
}
public function SaveItemList () {
var temp : String = "";
for(var i : int = 0; i< ItemList.Count; i++) {
if (i != ItemList.Count - 1)
temp += ItemList[i].ToString() + "*";
else
temp += ItemList[i].ToString();
}
PlayerPrefs.SetString("Items", temp);
}
public function SaveCheatCodeList () {
var temp : String = "";
for (var i : int = 0; i < CheatCodesList.Count; i++) {
if (i != ItemList.Count - 1)
temp += CheatCodesList[i].ToString() + "*";
else
temp += CheatCodesList[i].ToString();
}
PlayerPrefs.SetString("Items", temp);
}
Here is the error : Assets/Player/Scripts/Warehouse Player/WarehouseHoster.js(6,63): BCE0018: The name 'CheatCodeArray' does not denote a valid type ('not found').
It's the same 2 times for the 2 list.
A Type is not a variable, it is a component, class or script.
I cannot see anywhere you are using the list anyway (it is really hard to tell what you are trying to do). It seems you are trying to read the split string, which is stored in the arrays, not the List.
Your answer
Follow this Question
Related Questions
Strategy for saving game 1 Answer
A node in a childnode? 1 Answer
How do I save and retrieve ordered data? Can I do this with an array? 1 Answer
ReadLine to a Variable 2 Answers
Instantiate to temp var or directly adding to list? 1 Answer