- Home /
save load list
Hello I'm still learning and this is first time that I must save and load list.
I'm doing this: PlayerPrefs.SetInt ("lista",listItems[i]); (bottom of script)
I don't know how can I load this list in another scene.
this is script where I save list
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class wyborPrzedmiotow : MonoBehaviour {
public GameObject[] items;
public int itemCounter = 0;
private int curIndex;
public int maxImages;
public string xName = "items";
public bool a;
public bool a2 = false;
public GameObject Grid;
public List<int> listItems;
// Use this for initialization
void Start () {
List<int> listItems = new List<int>(new int[] {});
// You can add additional tests also I would enclose the instantiation inside a method then that would probably be useful as you could then call it from other places
maxImages = 5;
for (int i = 0; i < maxImages; i++) {
if (items.Length != 0 && items [curIndex] != null) {
curIndex = Random.Range (0, items.Length);
xName = "items" + curIndex;
foreach (Transform child in transform) {
if (child.name == xName) {
a = true;
//print ("true");
}
}
while (a == true)
{
curIndex = Random.Range (0, items.Length);
//elementsToExclude.Add (curIndex);
xName = "items" + curIndex;
a = false;
foreach (Transform child2 in transform) {
if (child2.name == xName) {
a = true;
// print ("true");
}
}
}
if ( a == false)
{
GameObject currentItem = (GameObject)Instantiate (items [curIndex], Vector3.zero, Quaternion.identity);
currentItem.transform.parent = Grid.transform;
currentItem.name = string.Format ("items" + curIndex);
listItems.Add (curIndex);
//itemCounter++;
}
}
Debug.Log(listItems[i]);
PlayerPrefs.SetInt ("lista",listItems[i]);
}
}
// Update is called once per frame
void Update () {
}
}
Answer by VesuvianPrime · Feb 05, 2015 at 06:26 PM
There is an excellent library here that provides functionality for storing collections in prefs.
Answer by KazooCar69 · Feb 05, 2015 at 06:27 PM
The way I pass data between scenes is by using DontDestroyOnLoad(), but you're probably looking for something more elegant.
Answer by Darioszka · Feb 05, 2015 at 07:11 PM
hmm yes DistortedShadow :) but I think I do this with something like this from link to library of VesuvianPrime
var numberArray = new int[10];
for (n in numberArray) n = Random.Range(-10, 11);
PlayerPrefsX.SetIntArray ("Numbers", numberArray);
Your answer
Follow this Question
Related Questions
List Contains false after playmode 0 Answers
How to save a progress by user with diferent users 2 Answers
Save and Load Function 2 Answers
How i can PlayerPrefs List<>? 0 Answers
How to save and load `List myList`? 1 Answer