Search Unity

Resolved NullReferenceException after trying to Enqueue object from another script.

Discussion in 'Scripting' started by Chickenbob5, Jun 8, 2022.

  1. Chickenbob5

    Chickenbob5

    Joined:
    Apr 1, 2022
    Posts:
    6
    I have an object pooler in my game to spawn objects into the game. I have to separate scripts that call functions from the object pooler script, one to spawn objects (by dequeuing and setting to active), and one to add them back to the queue. The problem is in trying to Enqueue. This is the part in the object pooler script where the function is set:
    Code (CSharp):
    1.     public GameObject Deleter(string tag, GameObject gameObject)
    2.     {
    3.  
    4.         poolDictionary[tag].Enqueue(gameObject);
    5.         gameObject.SetActive(false);
    6.  
    7.         return gameObject;
    8.     }
    Then it is called in this script:
    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.  
    4.         objectpooler.Deleter("Ibeam", gameObject);
    5.  
    6.  
    7.     }
    objectpooler is the variable that stores the object pooler script on this script. When I run the game the objects spawn but then on the collision it gives me NullReferenceException. I went to a thread on how to fix that but I still didn't figure it out.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,998
    This is the first mystery for us as we have no idea how it is implemented and if it works correctly.

    This is the second mystery as we don't know when and how you may assign this variable.

    This is the third mystery as it matters where exactly the NullReferenceException happened. Is it inside your "OnCollisionEnter2D" method? If that's the case, your "objectpooler" variable is null. If it's inside the "Deleter" method it's either that you don't have your poolDictionary set to anything or you have some major error in your pool logic. Assuming "poolDictionary" is a normal generic dictionary, if the dictionary exists it can not really return null unless you actively stored a null value inside the dictionary with that key. Because if the key does not exist you would get a MissingKey exception.

    In any case, that Deleter method is not really a great implementation for a pool.You blindly access any key that is passed in. This opens many possibilities for issues. Proper error handling should be applied here.

    Anyways, as Kurt said, you should figure out where exactly the exception happened in order to even start fixing it. Such an exception comes with a stack trace, file names and line numbers. You haven't shared any of those. Even if you had there's little we can do since we don't have the files and don't know what's in those lines
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    ALSO: do you even NEED an object pooler?

    I have never bothered in any of my games.

    Unity is an absolute BEAST of a game engine.

    If you wanna wrestle with one, go nuts. I would never reach for a pooler unless I proved I needed it.

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  5. Chickenbob5

    Chickenbob5

    Joined:
    Apr 1, 2022
    Posts:
    6
    Ya, that's probably a good idea.