- Home /
Question by
Itsgrain42 · Aug 23, 2017 at 09:45 PM ·
prefabssave datastrange iocclones
(Re-posted) How to save multiple of the same prefab clones
(I did re-post this one, no one managed to see the last one) Is it possible? What I mean is the position they are in and saving them so they can be viewed once exiting then entering, saving multiple of the same prefab. I have my SaveManager code here:
public GameObject CHP1; public FuelBar HP1; public GameObject CHP2; public FuelBar1 HP2; public GameObject CHP3; public CoolBar HP3; public GameObject CHP4; public FuelBar2 HP4; public GameObject CHP5; public CoolBar1 HP5; //public float CurrentHp;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
HP1 = CHP1.GetComponent<FuelBar> ();
HP2 = CHP2.GetComponent<FuelBar1> ();
HP3 = CHP3.GetComponent<CoolBar> ();
HP4 = CHP4.GetComponent<FuelBar2> ();
HP5 = CHP5.GetComponent<CoolBar1> ();
if(Input.GetKeyDown(KeyCode.O)){
Save ();
//CurrentHp = HP1.CurrentHp;
}
if(Input.GetKeyDown(KeyCode.P)){
Load ();
}
}
public void Save()
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fs = new FileStream("save1.bin", FileMode.Create, FileAccess.Write))
{
binaryFormatter.Serialize(fs, HP1.CurrentHp);
binaryFormatter.Serialize(fs, HP2.CurrentHp);
binaryFormatter.Serialize(fs, HP3.CurrentHp);
binaryFormatter.Serialize(fs, HP4.CurrentHp);
binaryFormatter.Serialize(fs, HP5.CurrentHp);
}
}
public void Load()
{
if (!File.Exists("save1.bin"))
return;
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fs = new FileStream("save1.bin", FileMode.Open, FileAccess.Read))
{
HP1.CurrentHp = (float)binaryFormatter.Deserialize(fs);
HP2.CurrentHp = (float)binaryFormatter.Deserialize(fs);
HP3.CurrentHp = (float)binaryFormatter.Deserialize(fs);
HP4.CurrentHp = (float)binaryFormatter.Deserialize(fs);
HP5.CurrentHp = (float)binaryFormatter.Deserialize(fs);
//HP1.CurrentHp = CurrentHp;
}
}
And the basic prefab clone spawning code:
public Transform[] spawnLocations;
public GameObject[] whatToSpawnPrefab;
public GameObject[] whatToSpawnClone;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.F)) {
whatToSpawnClone [0] = Instantiate (whatToSpawnPrefab [0], spawnLocations [0].transform.position, Quaternion.Euler (0, 0, 0)) as GameObject;
}
}
There is nothing wrong with this code, I was just wondering about the prefab thing. If you know a way, please help!
Comment