Search Unity

Bug Null References ONLY in Build

Discussion in 'Editor & General Support' started by DrewofDonuts, Aug 30, 2022.

  1. DrewofDonuts

    DrewofDonuts

    Joined:
    Apr 23, 2020
    Posts:
    28
    Hello,

    I am receiving game-breaking null reference exceptions in my build. In the editor, there are no issues. I only have two scenes to test between - the scene that is breaking is larger (assets/lighting/etc). The scene that doesn't break is essentially gray boxed, with the exception of the characters game objects

    In other words - what could lead to null reference exceptions in build, but not in the editor?

    *Resolved*
    FindingByType calls were executed in a different order in Build, leading to a nullreference.
     
    Last edited: Feb 2, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Plenty of things. But without more information we can't really help you. What in particular is null? Perhaps an example of the code that is throwing the error, and the result you expect?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Just a few possibilities (non exhaustive list):
    • You might have code which depends on the order of certain functions being executed. For example a Start which depends on another Start having run first. Such things are not guaranteed.
    • You might have code which depends on the order of objects returned in a function like GetComponentsInChildren. This is also not guaranteed.
    • You might have code which is framerate dependent - and fails at a higher or lower framerate.
    • You might have platform-dependent code. For example code which relies on the existence of particular hardware or devices.
     
  4. DrewofDonuts

    DrewofDonuts

    Joined:
    Apr 23, 2020
    Posts:
    28
    Thank you for the replies! Just updated my post, as I've pinpointed the issue, but haven't determined the solution.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Is the player in its own additive scene? Or are they instantiated into the same scene as everything else?

    If the former, I would use some globally accessible manner to determine the player's position, easiest being a singleton. In the latter, perhaps moving this search to Start will work, as Start is called once everything has been fully realised into the scene.
     
    PraetorBlue likes this.
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Be more specific. Show us the code. Show us how you're finding the object. Show us what else is going on? Does the object exist in the scene in the editor? Is it being instantiated at runtime somewhere?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Build execution orders often show you that your code is subtly dependent on one script initializing before the other.

    Strive to fix those types of dependencies so your code will defer accessing things that haven't been initialized.

    Otherwise, it's still always the same three steps, and it sounds like you might have done Step #1.

    Hurry onto Step #2 below so you can get to Step #3 and finish the problem.

    Nothing special about a null reference!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that

    Whatever it is, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    DrewofDonuts likes this.
  8. hakimo001

    hakimo001

    Joined:
    Oct 20, 2019
    Posts:
    1
    for me i solved just by remove findGameObjectWithTag and replace it with attaching gameobject directly
     
    turtle734 likes this.
  9. turtle734

    turtle734

    Joined:
    Aug 30, 2022
    Posts:
    1
    Thank you. I should've thought of this after 4 hours but somehow I didn't. Thank you
     
  10. khiemgluong

    khiemgluong

    Joined:
    Jan 6, 2023
    Posts:
    22
    Hey everyone, so I encountered a problem with getting null references to a camera object but only in the build. This may not be applicable to every case, but it was simply because I had its tag set to "EditorOnly", which obviously makes the gameobject only referenceabke in the editor. Changing it to "Untagged" or anything else fixed the problem.