- Home /
how to fix missing reference exception when reload scene
MissingReferenceException: The object of type 'RecycleGameObject' 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. ObjectPool.NextObject (Vector3 pos) (at Assets/Scripts/ObjectPool.cs:28) GameObjectUtil.Instantiate (UnityEngine.GameObject prefab, Vector3 pos) (at Assets/Scripts/GameObjectUtil.cs:17) Spawner+c__Iterator0.MoveNext () (at Assets/Scripts/Spawner.cs:34) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
these are my codes
public class ObjectPool : MonoBehaviour {
public RecycleGameObject prefab;
private List<RecycleGameObject> poolInstances = new List<RecycleGameObject> ();
private RecycleGameObject CreateInstance(Vector3 pos){
var clone = GameObject.Instantiate (prefab);
clone.transform.position = pos;
clone.transform.parent = transform;
poolInstances.Add (clone);
return clone;
}
public RecycleGameObject NextObject(Vector3 pos){
RecycleGameObject instance = null;
foreach (var go in poolInstances) {
if (go.gameObject.activeSelf != true) {
instance = go;
instance.transform.position = pos;
}
}
if (instance == null) {
instance = CreateInstance (pos);
}
instance.Restart ();
return instance;
}
}
another one
public interface IRecycle {
void Restart ();
void Shutdown ();
}
public class RecycleGameObject : MonoBehaviour {
private List<IRecycle> recycleComponents;
void Awake () {
var components = GetComponents<MonoBehaviour> ();
recycleComponents = new List<IRecycle> ();
foreach (var component in components) {
if (component is IRecycle) {
recycleComponents.Add (component as IRecycle);
}
}
}
public void Restart () {
gameObject.SetActive (true);
foreach (var component in recycleComponents) {
component.Restart ();
}
}
public void Shutdown () {
gameObject.SetActive (false);
foreach (var component in recycleComponents) {
component.Shutdown ();
}
}
}
and another one
public class GameObjectUtil {
private static Dictionary<RecycleGameObject, ObjectPool> pools = new Dictionary<RecycleGameObject, ObjectPool> ();
public static GameObject Instantiate(GameObject prefab, Vector3 pos){
GameObject instance = null;
var recycelScript = prefab.GetComponent<RecycleGameObject> ();
if (recycelScript != null) {
var pool = GetObjectPool (recycelScript);
instance = pool.NextObject (pos).gameObject;
} else {
instance = GameObject.Instantiate (prefab);
instance.transform.position = pos;
}
return instance;
}
public static void Destroy(GameObject gameObject){
var recycleGameObject = gameObject.GetComponent<RecycleGameObject> ();
if (recycleGameObject != null) {
recycleGameObject.Shutdown ();
} else {
GameObject.Destroy (gameObject);
}
}
private static ObjectPool GetObjectPool(RecycleGameObject reference){
ObjectPool pool = null;
if (pools.ContainsKey (reference)) {
pool = pools [reference];
} else {
var poolContainer = new GameObject (reference.gameObject.name + "ObjectPool");
pool = poolContainer.AddComponent<ObjectPool> ();
pool.prefab = reference;
pools.Add (reference, pool);
}
return pool;
}
}
at first, it spawns very fine,, but when i click a restart button from my gameover ui, the error occurs... I'm not totally good in programming, the object pool codes came from the tutorials and i just implemented it to my game...
Answer by ShadyProductions · Dec 06, 2017 at 05:03 PM
If your restart button destroys gameobjects,
you should call .Clear();
on poolInstances
when you restart.
where would i put the code?
my restart button contains: public void Reload (string scene) { Scene$$anonymous$$anager.LoadScene (scene); }
GameObjectUtil.pools.Clear();
is gonna be it I think. I believe the static reference lingers through load scene
where exactly would i put that code? because I can only put that code on the GameObjectUtil class... the GameObjectUtil.pools.Clear();
... and other classes cant access the .Clear()
make pools dictionary public and you can make it a get; only
it finally worked! thank you very much!
but i do have another problem... haha
Your answer
Follow this Question
Related Questions
Load Scene Bug 0 Answers
JumpPad script for a JumpPad like Geometery dash 1 Answer
Accessing a script only using its string 3 Answers
My life counter is not updating 1 Answer