- Home /
Saving multiple objects with same tag
Hello! I'm making a game with multiple cloned objects. I need to start on the saving system, and don't really know where to start. I'm useing Javascript, and making the game for android. I don't want to use playerprefs, and I'm stumped on how to get all the objects saved to a single file. I'm thinking that I'd need to have unity find all the objects with the same tag, and use a loop to save them to a file, but I don't know how to go about that. You're time is appreciated! thank you!
Answer by clunk47 · Dec 10, 2012 at 06:42 AM
Well, I know C#, JS too, but have forgotten most JS. You'd need to create a list or array. Here's a c-sharp example, I'll link you to the script reference at the bottom so you can read up on arrays and lists for JS.
using UnityEngine;
using System.Collections;
public class ArrayTest : MonoBehaviour
{
GameObject[] objects;
void Start()
{
objects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
Debug.Log(objects.Length);
}
}
That's if you want to find ALL objects in the scene. If you want to find objects by tag,
objects = GameObject.FindGameObjectsWithTag("yourTagName");
Then say each object has a different name and you want to find each one of them.
foreach(GameObject obj in objects)
{
Debug.Log(obj.name);
}
It's one thing I haven't ever actually completed, but I've been trying to code around in my head at night, and I think I could figure it out. I have to do this for my own project anyway, so I'd be happy to share with you any sucessful implementations. Add me on skype Clunk47 and I'll let you know when I have something going.
Alright. Thanks for the help. I myself have been playing around with a few different concepts. Sinceis somet th ing ive never done before as you can imagine its a pain. Im sure we'll figure something out. In all honesty it doesnt appear to be all that hard to do the saving, but what im unsure of how im going to do it loading, but since with saving its looping wouldnt it continuously loop through and just keep rewriting the same planets over and over again, or would it stop once it hits the last one?
Well really we wouldn't have to use loops, we could serialize everything as it happens, and store some type of variable to a text, dat, or registry file, then just append new lines. Then on load, check those lines, but make a variable to check each specific line in the txt file. Like location of player could be stored at line[15] or whatever... I'll try to get something started today.
But yeah just hit me up on skype sometime man, u never know maybe we could work on a small project together sometime, I'm always down for anything profitable :D