- Home /
Collectables across multiple levels
I'm creating a project where the objective is to find objects in separate levels, however apart from having a GameObject
that doesn't get destroyed I'm unsure where to go from here. I need a system that goes checks a database when loading a level and only creates the collectables that have not been found yet or destroys the collectables in the level that have already been found once the level is loaded.
Can anyone point me in the direction of a good place to start? The system needs to work on all of the mobile platforms (Windows Phone, iOS, Android, and Blackberry 10) and be possible in JS. I've read a bit about XML files however most tutorials are not built for those who are a newcomers of XML.
[Edit for Clarity] When I say database I meant a local file that stores the data for an object in the scene, basically in Unity I have 5 levels, in each level there are 3 collectables. When a level is loaded I want to have an array of GameObject called Collectables in a GameController object and use GameObject.FindGameObjectsWithTag("Collectable");
to populate that array. After that is done I want to read some sort of data file, the file needs to have an ID number for each level to only check the data that corresponds with the the id of the current level in Unity and an ID for each collectable, and then go through the data for each Collectable from the file, if a Collectable has the IsCollected value of true then it should destroy the GameObject from the Collectables array in the scene. I've done my best to write an XML that best portrays what I need in the data Here.
Answer by Sisso · Sep 03, 2013 at 12:54 PM
Give a unique name for each collectable, and put script like this:
function Awake() {
if (PlayerPrefs.GetString("collectables."+gameObject.name, "false") == "true") {
GameObject.Destroy(gameObject);
}
}
// it could be called directly or replaced by a trigger event
function Collect() {
PlayerPrefs.SetString("collectables."+gameObject.name, "true");
}
After you call Collect, it will register on playerprefs and it will self destroy on awake.
Some usefull references:
PlayerPrefs
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
Colliders and triggers
http://docs.unity3d.com/Documentation/ScriptReference/Collider.html
Ouch, but I'll take that with a grain of salt, this is my first question on the site and may be taken for a complete newbie. I can program gameplay mechanics just fine and have completed half of my gameplay scripting (Fully working player, camera, enemies and cutscenes), I was looking for something more geared towards data handling, I've always been a gameplay programmer and this is my first venture into handling saving game data.
Look, I really like to help if I can. But you be specific in one point.
What the relation of database? It is a local database? Remote database? All users share the same database? It is a client-server multiplayer? Need to be a database or could be any persistent storage? X$$anonymous$$L? What do you want with X$$anonymous$$L? Comunicate? Storage? What relation of X$$anonymous$$L and a mobile plataform? Edit the level using X$$anonymous$$L? Ouou... stack overflow :P
Or I could throw everything out and back to "how do you create a level where the already colletable objects don't appear?". It could be solved by one script, less that 10 lines of code and PlayerPrefs.
I think the last part of your comment sums it up nicely: How do I have collectables that don't appear if they have already been collected?
Look at UnitySerializer http://whydoidoit.com/unityserializer/
Thanks, I'll have a look at that and also the UnitySerializer page that fafase suggested, If I can limit the saving to just the objects tagged 'Collectable' then that seems like it would be the way to go :)
Answer by ragnaros100 · Sep 03, 2013 at 02:23 PM
You can look at my series of questions I did some time ago:
http://answers.unity3d.com/questions/279650/create-text-file-in-c.html
This will save on the machine, not on a db. I think he wants a db connection. I could be wrong though.
Answer by fafase · Sep 03, 2013 at 02:23 PM
Here is how to access and update db: http://wiki.unity3d.com/index.php?title=Server_Side_Highscores
Then you need to collect the info into a class and check what the player has done.
if(level1 == Level.Done){//Allow access to level 1}
if(level2 == Level.Done){//Allow access to level 2}
The rest is always the same, has he done it, if yes, allow something.
All you need is to get the script from the link working.