I'm creating a procedural generated game, which will generate new "terrain" forever, do to this my game starts to lag, so I'm trying to unload some chunks, which I'd later like to load again (if the player comes back to that specific position again)...
This is my code for unloading:
GameObject _tmp = MapGeneration.Map[Mathf.Abs(y)][x];
DestroyImmediate(MapGeneration.Map[Mathf.Abs(y)][x].gameObject);
MapGeneration.Map[Mathf.Abs(y)][x] = _tmp;
Later when I try to instantiate the block again:
Instantiate(MapGeneration.Map[Mathf.Abs(y)][x].gameObject, new Vector3(x, y, 0), Quaternion.identity);
I get this error:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) MapGeneration.Update () (at Assets/Scripts/MapGeneration.cs:53)