Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lsp34 · Dec 06, 2017 at 04:50 PM · scripting problemunity 2dscene-loadingreferenceobject pool

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lsp34 · Dec 06, 2017 at 05:10 PM 0
Share

where would i put the code?

my restart button contains: public void Reload (string scene) { Scene$$anonymous$$anager.LoadScene (scene); }

avatar image ShadyProductions lsp34 · Dec 07, 2017 at 07:55 AM 0
Share

GameObjectUtil.pools.Clear(); is gonna be it I think. I believe the static reference lingers through load scene

avatar image lsp34 · Dec 07, 2017 at 12:43 PM 0
Share

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()

avatar image ShadyProductions lsp34 · Dec 07, 2017 at 03:09 PM 0
Share

make pools dictionary public and you can make it a get; only

avatar image lsp34 ShadyProductions · Dec 07, 2017 at 04:10 PM 0
Share

it finally worked! thank you very much!

but i do have another problem... haha

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to make player not movable when paused? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges