Basically i am making a game which has three levels which connect to a homeworld each level has for example 100 items to collect. when the player collects an item in the level and i leave the level and return i want that item to not be there.
so basically my problem is when i collect items in a level, go to home, come back all the items reappear which i dont want. when an item is collected i want them to be gone and not come back when switching between levels.
like ive placed 100 items in my scene could i assign a unique id to each item and then kindv save the collected items ids in a array so when the scene is loaded again items with that id are destroyed
sorry if thats confusing. if you could point me in the right direction that would be good or give an example thanks im new to this.
Im writting in c# if that matters.
If your player is don’t destroy on load then the easiest is to have an array of bools for each level in a script attached to the player, call it something like levelOnePickedUp, when the level loads access the player script and load only those items that are still false. When you pick one up set the corresponding array item to true.
You will need to save in player prefs as @Klarax says but for that you want this:
[ArrayPrefs][1]
Both C# and Javascript/boo examples in that page.
EDIT
OK so I created 64 empty GameObjects, highlighted them all and tagged them as Coin.
using UnityEngine;
using System.Collections;
public class PlayerPrefsXTest : MonoBehaviour{
private Vector3[] coinPos = new Vector3[64]; // Assumes you have exactly 100 objects
private Quaternion[] coinRot = new Quaternion[64];
private bool[] coinCollected = new bool[64];
private int countCoins;
void Start()
{
countCoins = 0;
foreach(GameObject go in GameObject.FindGameObjectsWithTag("Coin"))
{
coinPos[countCoins] = go.transform.position;
coinRot[countCoins] = go.transform.rotation;
coinCollected[countCoins] = false;
go.name = countCoins.ToString();
countCoins ++;
}
PlayerPrefsX.SetVector3Array("levelOnePos", coinPos);
PlayerPrefsX.SetQuaternionArray("levelOneRot", coinRot);
PlayerPrefsX.SetBoolArray("levelOneCollected", coinCollected);
// coinCollected = PlayerPrefsX.GetBoolArray("levelOneCollected");
// coinPos = PlayerPrefsX.GetVector3Array("levelOnePos");
// coinRot = PlayerPrefsX.GetQuaternionArray("levelOneRot");
for (int i = 0; i < coinCollected.Length; i++)
{
Debug.Log("Coin Number " + i + " Position = " + PlayerPrefsX.GetVector3Array("levelOnePos") *+*
_ " Rotation = " + PlayerPrefsX.GetQuaternionArray(“levelOneRot”) +_ _ " Collected = " + PlayerPrefsX.GetBoolArray(“levelOneCollected”));
* } } } This script uses that PlayerPrefsX script to store all the Coin positions and quaternion rotations. as well as renames all the coins to a number. It also stores if that number has been collected already in a bool[] Array. Really you only need the last one if you want to just use set active to show or hide the coins but, it makes a lot of sense to instantiate the coins when the level loads. That call is yours though, just make sure you understand what the above script does, you’ll need to change the number from 64 to however many you want. Feel free to ask questions, and I don’t expect this to be the last update for this just want to make sure I don’t give too much info in one go. [1]: http://wiki.unity3d.com/index.php/ArrayPrefs2*_
There are a bunch of other ways to save data that don’t involve player prefs. See the tutorial for more details.
One approach I have used in the past is to serialise an array, then converted the resulting bits to a string. That string can then be saved to player prefs, a file or a server and reloaded again at will.