- Home /
Instantiating array in GameManager upon loading Scene
I have two scenes. One scene is world map that you can navigate with a marker that represents the player. On this world map there are enemy markers that represent groups of enemies you can encounter if their 2D colliders hit. Once these two colliders hit, it will load the second scene, the battle scene.
Before the battle scene is loaded, I first run a method in GameManager that finds all gameObjects that have the EnemyMarker script and store them in an array (GameObject enemyMarkers[]). I also record the locations of each enemy marker as well in GameManager. My issue is that after the battle is over and I load the WorldMap scene again, my method that instantiates all of those enemy markers to their previous transform.location says all items in my array are suddenly null. I use Debug.Log to insure that the objects saved to my array are indeed the objects I intended to save. My GameManager is set to DontDestroyOnLoad as well.
I am still only a few weeks into learning c# and Unity so I am sure that I am missing a concept. My goal is to place those enemy markers right where they were before the battle scene loaded. These instances of enemy markers hold important information that are unique to each marker (i.e. how many enemies are this enemy group) so I don't want to simply instantiate a prefab because those variables will likely change due to the RNG used to determine said variables. Any suggestions? Code provided below:
public class GameManager : MonoBehaviour
{
public static GameManager instance;
private PlayerMarker pm;
public Item[] equippedItems = new Item[11];
[Header("Enemy Encounter Info")]
public int numberOfMeleeEnemies;
public int numberOfRangedEnemies;
public string enemySize;
[Header("Save Between Scenes")]
private GameObject[] enemyMarkers;
private Vector2[] previousMarkerPositions;
private Vector2 previousPlayerPosition;
public bool battled;
private void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if(battled)
{
LoadScene();
battled = false;
}
}
public void SaveScene()
{
EnemyMarker[] eMarkers = FindObjectsOfType<EnemyMarker>();
Debug.Log("Number of eMarker Objects Found: " + eMarkers.Length);
enemyMarkers = new GameObject[eMarkers.Length];
previousMarkerPositions = new Vector2[eMarkers.Length];
pm = FindObjectOfType<PlayerMarker>();
for (int i = 0; i < enemyMarkers.Length; i++)
{
enemyMarkers[i] = eMarkers[i].gameObject;
previousMarkerPositions[i] = enemyMarkers[i].transform.position;
}
Debug.Log("Number of enemy markers in array: " + enemyMarkers.Length);
Debug.Log("Name of first saved enemy: " + enemyMarkers[0]);
previousPlayerPosition = pm.gameObject.transform.position;
}
public void LoadScene()
{
Invoke("UpdateScene", 0.1f);
}
private void UpdateScene()
{
Time.timeScale = 1;
for (int i = 0; i < enemyMarkers.Length; i++)
{
if(enemyMarkers[i] != null)
{
Instantiate(enemyMarkers[i], previousMarkerPositions[i], Quaternion.identity);
Debug.Log("Enemy Marker " + i + " instantiated!");
}
}
pm = FindObjectOfType<PlayerMarker>();
pm.gameObject.transform.position = previousPlayerPosition;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613031214im_/https://answers.unity.com/themes/thub/images/avi.jpg)