- Home /
MissingReferenceException:
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.
I know many other have already asked this kind of question, but still I haven't found an answer which worked for me.
The issue is:
whenever i reset my scene by using SceneManager.LoadScene(2); this error is shown : "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 Script is:***
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; //error
poolInstances.Add(clone);
return clone;
}
public RecycleGameObject NextObject(Vector3 pos)
{
RecycleGameObject instance = null;
foreach (var go in poolInstances)
{
if (go.gameObject.activeSelf != true) //error
{
instance = go;
instance.transform.position = pos;
}
}
if (instance == null)
instance = CreateInstance(pos);
instance.Restart();
return instance;
}
}
RecycleGameObject Script:
public interface IRecyle{
void Restart();
void Shutdown();
}
public class RecycleGameObject : MonoBehaviour
{
private List<IRecyle> recycleComponents;
void Awake()
{
var components = GetComponents<MonoBehaviour> ();
recycleComponents = new List<IRecyle> ();
foreach (var component in components)
{
if (component is IRecyle)
{
recycleComponents.Add(component as IRecyle);
}
}
}
public void Restart()
{
gameObject.SetActive(true);
foreach (var component in recycleComponents)
{
component.Restart();
}
}
public void Shutdown()
{
gameObject.SetActive(false); //delete
foreach (var component in recycleComponents)
{
component.Shutdown();
}
}
}
Your answer
Follow this Question
Related Questions
Move the camera in 2D when target reaches bounds. Is there a better way? 0 Answers
how to move child object inside a moving parent in 2d using Vector2.MoveTowards()? 1 Answer
How can i create multiplayer game using wifi hotspot in unity 0 Answers
Game for 2 players using 2 android devices 0 Answers
GDPR unity and Admob ads 0 Answers