Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] MissingReferenceException, even though object exists

Discussion in 'Scripting' started by TehGM, Sep 20, 2016.

  1. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Hi, it's me again, with yet another Unity issue for your (questionable) entertainment!

    To start with - I have box class which has PriceText member
    Code (csharp):
    1. public class ShoppingOrderBox : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private PriceText _priceText;
    5.     /* . . . */
    The box has method which accesses price text
    Code (csharp):
    1. private void Method()
    2. {
    3.     _priceText.gameObject.SetActive(!double.IsInfinity(Order.MaxPrice));
    4.     /* . . . */
    5. }
    And one property that also calls the method
    Code (csharp):
    1. public ShoppingOrder Order
    2. {
    3.     get { return _order; }
    4.     set { /* . . . */ Method(); }
    5. }
    box is created by another class using a prefab. prefab has _priceText properly attached.
    Code (csharp):
    1. ShoppingOrderBox box = Instantiate<ShoppingOrderBox>(_orderBox);
    2. box.Order = order;
    Everything works at first. Then I reload the scene - boxes get recreated exact same way from the same class.
    Method() triggered by Order property doesn't cause any trouble. All changes from Method() are reflected in gameObject's appearance. PriceText exists in hierarchy as expected, and also is referenced properly and pointing to correct created object (checked that using Unity Editor).
    However, if I call Method() on any point later, I get MissingReferenceException.
    Code (csharp):
    1. MissingReferenceException: The object of type 'PriceText' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    3. VRBH.UI.ShoppingOrderBox.Method () (at Assets/Code/GUI/ShoppingOrderBox.cs:52)
    What's even stranger, if I null-check first, exception doesn't happen, but gameObject still gets updated correctly (meaning (_priceText != null) returns true).
    Code (csharp):
    1. if (_priceText != null)
    2.     _priceText.gameObject.SetActive(!double.IsInfinity(Order.MaxPrice));
    3.     /* gameObject active state gets updated */
    I can live with that work-around, however I find this behaviour really weird. I made my code messy with all event handlers dereferencing in many (possibly not needed) places, thinking they were causing the issue
    but seems it was just that part.
     
  2. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    I feel so stupid now, but it actually had a lot to do with events - I was copying Orders using MemberwiseClone, and I missed the fact, that it also copies events - clearing them all after cloning helped.

    Oh well, I can't delete the thread now.
    However, behaviour of unity is still strange - how it was working just after object instantiation using property setter. Also, I added log messages on begining of the method, and they weren't fired - however, issue seems to be solved. Heh.

    We all learn on our own mistakes, no?