I'm making a game (specifically a top-down shooter) where there is a main area and several sub-areas saved as separate scenes. This has lead to a problem: if I kill an enemy in the main area, going into a side area, and then come back into the main area, the enemy will still be alive as if I'd never killed him. This is obviously problematic.
I've previously done this with static variables, but the problem here is that all the enemies are prefabs and use the same scripts, and I haven't successfully implemented a manifest-esque script (like the one suggested [here][1]). So, in brief, is there a way to keep track of enemy information like position and whether they're alive or dead so that when the player leaves and comes back to a scene, the enemies he killed stay dead?
Here's the code I have so far of the script to remember enemy positions, just in case there's something incredibly basic I'm missing.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyStorage {
public Vector3 lastPos;
public bool dead;
public EnemyStorage(Vector3 pos, bool d) {
dead = d;
lastPos = pos;
}
}
public class EnemyPositionsCompound : MonoBehaviour {
public static List enemySaves = new List();
public static GameObject[] enemies;
private bool correctLevel;
private static bool hasBuiltList; //check to see if the list has already been built
// Use this for initialization
void Start () {
Debug.Log("checking enemies "+" has build list? "+hasBuiltList);
//check to see if we're on the right level
if (Application.loadedLevelName == "Compound") {
correctLevel = true;
}
else {
correctLevel = false;
}
//if we are on the right level, run the shit
if (correctLevel) {
//get all enemies in the scene
enemies = GameObject.FindGameObjectsWithTag("Enemy");
if (!hasBuiltList) {
foreach (GameObject e in enemies) {
Debug.Log(e.name);
enemySaves.Add(new EnemyStorage(e.transform.position, e.GetComponent().dead));
}
hasBuiltList = true;
}
foreach (EnemyStorage s in enemySaves) {
Debug.Log("Enemy status: "+s.lastPos+" "+s.dead);
foreach (GameObject e in enemies) {
if (s.dead == true) {
e.GetComponent().dead = true;
}
}
}
}
}
// Update is called once per frame
void Update () {
}
}
[1]: http://answers.unity3d.com/questions/163092/suggestion-for-persisting-level-state-between-load.html
↧