Search Issue Tracker

Third Party Issue

Votes

2

Found in

2021.3.38f1

2022.3.27f1

6000.0.0f1

Issue ID

UUM-72568

Regression

No

"NullReferenceException: Object reference not set to an instance of an object" error is thrown in the Play Mode when the project is reopened with a Prefab GameObject selected in the SerializedField

--

-

How to reproduce:
1. Open the “bug test“ project
2. Open the “SampleScene“
3. Drag and Drop “Button“ Prefab to the Scene
4. Open the “Main Camera“ GameObject in the Inspector
5. In the “Game Manager (Script)“ Component add the “Button“ GameObject in the “Element 0“ field
6. Save the project and reopen Unity
7. Enter the Play Mode and observe the Console window

Expected result: No errors are thrown and clicks on the button are logged in the Console
Actual result: “NullReferenceException: Object reference not set to an instance of an object. GameManager.Start()” error throw, clicks on the button are not logged in the Console

Reproducible with: 2021.3.38f1, 2022.3.27f1, 6000.0.0f1

Reproducible on: macOS 13.5.2 (Intel), Windows 10 Pro (2H22)
Not reproducible on: No other environments tested

Workaround:
1. Open the “Button“ GameObject in the Inspector
2. Remove the “Button (Script)“ Component
3. Add “Button (Script)“ Component
4. Open the “Main Camera“ GameObject in the Inspector
5. In the “Game Manager (Script)“ Component add the “Button“ GameObject in the “Element 0“ field

Notes:
- After the workaround, the “Game Manager (Script)“ Component will function as expected until the next Unity reopening
- Only reproducible with Prefabs

  1. Resolution Note:

    By default Unity calls Start() in unspecified order between MonoBehaviours which means that we can call:
    GameManager.Start()
    Button.Start()
    or
    Button.Start()
    GameManager.Start()

    When GameManager.Start() is called before Button.Start() adding new events to Button.actions will result in a null pointer exception because Button.actions is not initialized. Initialization of Button.actions can be done in Button.Awake() instead of Button.Start() to make sure Button.actions is not null when GameManager.Start() is called.

    Another option is to use Script Execution Order settings (https://docs.unity3d.com/Manual/class-MonoManager.html) to make sure Button.Start() is called before GameManager.Start().

Comments (3)

  1. villarjohn3437

    May 30, 2024 16:48

    Excellently shown. I loved every quote, and I appreciate you sharing the information. Continue inspiring and sharing with others. By the way, I recommend checking out my blog on how to get Security Guard Services in Riverside at https ://frontlinepatrol. com/home-security/security-guard-services-near-me .

  2. villarjohn3437

    May 29, 2024 21:41

    Absolutely thrilled with the insightful content provided by the admin or author of this website! Their expertise shines through, particularly in their piece about how to sell used electronic devices at https ://www .gizmogo.com . It's informative, engaging, and a true delight to read. Keep up the fantastic work!

  3. gavinclester

    May 23, 2024 03:50

    Making a game with skins, this issue is making the skin selection process more difficult…

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.