Im testing out saving the position of multiple enemies. As of now i have the amount of enemies in a scene, but when i save it and load they all spawn on the same point. This is because the save method uses a tag, so i am unsure how i can increment that tag or if there is another way maybe using lists? i tried using lists but that would still result in me assigning a tag for each.
Attached is the code for the GameData, the saving & Loading. Thanks!
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameData : MonoBehaviour
{
[Header("Enemies")]
public List<GameObject> enemies;
public GameObject[] amountOfEnemies;
public void Awake()
{
amountOfEnemies = GameObject.FindGameObjectsWithTag("Enemy");
}
}
Code: Select all
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveSystem : MonoBehaviour
{
public ManagerScript ms;
public GameData gd;
public Transform player;
public void Save()
{
ManagerScript script = ms.GetComponent<ManagerScript>();
ES3.Save("Current Ammo In MP5", script.mp5._currentAmmo);
ES3.Save("Player Position" , player.transform.position);
ES3.Save("Current Weapon", script.awR.playersActiveWeapon);
ES3.Save("9MM Reserve", script.playerAmmoBag.nineMM);
GameData gdContainer = gd.GetComponent<GameData>();
foreach(GameObject enemies in gdContainer.amountOfEnemies)
{
ES3.Save("EnemeyPostion", enemies.transform.position);
Debug.Log("Saving enemy");
}
}
}
Code: Select all
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadSystem : MonoBehaviour
{
public ManagerScript ms;
public Transform player;
public GameData gd;
public void Load()
{
player.transform.position = ES3.Load<Vector3>("Player Position");
ManagerScript script = ms.GetComponent<ManagerScript>();
script.awR.playersActiveWeapon = ES3.Load<activeWeapon>("Current Weapon");
script.mp5._currentAmmo = ES3.Load<int>("Current Ammo In MP5");
Debug.Log("Loading " + script.awR.playersActiveWeapon.ToString());
script.playerAmmoBag.nineMM = ES3.Load<int>("9MM Reserve");
GameData gdContainer = gd.GetComponent<GameData>();
foreach(GameObject enemies in gdContainer.amountOfEnemies)
{
enemies.transform.position = ES3.Load<Vector3>("EnemeyPostion");
Debug.Log("Loading enemy");
}
}
}