- Home /
Save array of GameObjects and their variables from an attached script under a parent
I have built a map editor for my game. It consists of an array of 3d blocks on a flat plane (x,0,z positions). Each of these blocks have a script attached to them that store the following values:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlankTile : MonoBehaviour {
public int x = 0;
public int y = 0;
public int tileStyle = 0;
public int walkable = 0;
public int wall = 0;
public int spawnCreature = 0;
public int spawnObject = 0;
public int spawnInteractive = 0;
public int triggerId = 0;
}
I have the values getting set fine, the blocks getting displayed and editable fine. But I want to save this data to a file (that isn't easy to modify) to create all my game levels. So I need to save the location (also stored in the public x and y of the script) and be able to load them whenever I need to recreate the block locations and values. I would also like to give it a file name from the text box above Load and Save buttons Any help would be really appreciated.
Are there any asset store items that do saving/loading of this type?
Answer by FuzzyLogic · Feb 27, 2018 at 02:48 PM
This tutorial provides a good introduction to a basic save and load mechanism.
http://catlikecoding.com/unity/tutorials/hex-map/part-12/
It is part 12 of a much larger series (which I highly recommend) so it assumes that you have already completed the previous 11 parts, though I think the information is explained well enough that one should be able to cherry-pick the information.
The method described is meant to be simple for learning purposes so is not the best way but there's nothing really wrong with it either and should be sufficient for a semi-simple game.
Nice mate! This has everything I need. I will have to run through it step by step but viewing each of the steps on the fly encompasses all components of what I am trying to achieve.
You can download the working unity project from the bottom of that page to follow along with that part of the tutorial and see how its actually implemented in code. http://catlikecoding.com/unity/tutorials/hex-map/part-12/smaller-file-size/smaller-file-size.unitypackage