0

here are some pretty simple lines of code, that throw a NullReferenceException. Since I get the exception via CloudDiagnostics I don't know which line threw the exception, but only which method.

private void DestroyChest() {
    if (Chest != null && Chest.gameObject == null) {
        Debug.LogException(new Exception("Chest.Gameobject is null!"));
    }

    Destroy(Chest?.gameObject);
}

Chest is a MonoBehavior script. And nowhere is a property accessed without checking it first.

Now my question is. Why and how do I get a NullReferenceException.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
T. Grumser
  • 99
  • 3
  • 18
  • My guess: `Destroy` doesn't like `null` values being passed to it. – ProgrammingLlama Nov 24 '21 at 09:51
  • 1
    @Llama no actually Unity doesn't like `Chest?` ;) – derHugo Nov 24 '21 at 09:51
  • Rather use `if(Chest) Destroy(Chest.gameObject);` (see the duplicate link) – derHugo Nov 24 '21 at 09:53
  • Destroy ignores `null` passed to it and unity is single threaded. – T. Grumser Nov 24 '21 at 09:53
  • @derHugo please remove the association. If this would be the issue described in the associated post, i would get a `MissingReferenceException`. But since i get a `NullReferenceException` something else seems to be happening. – T. Grumser Nov 24 '21 at 10:02
  • @T.Grumser no ... the `MissingReferenceException` only applies to the same frame right after `Destroy` where `Chest` is in that in-between state. Once the GarbageCollector does it's job the `Chest` will be `null` and you would get a `NullReferenceException` using `Chest?.gameObject` ... as said use `if(Chest) Destroy(Chest.gameObject);` and you should be fine in the future – derHugo Nov 24 '21 at 10:04
  • Why would i get a null refference exception when Chest is `null` and i use the ?-operator? – T. Grumser Nov 24 '21 at 10:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239536/discussion-between-t-grumser-and-derhugo). – T. Grumser Nov 24 '21 at 10:07
  • I can't tell you the why exactly ... can't you just believe me and **never** use null checks and even less null conditionals/coalescings but rather simply the mentioned `bool` operator for just anything that inherits from `UnityEngine.Object`? ;) – derHugo Nov 24 '21 at 10:20
  • Ok actually I now remember why this happens: While `if(Chest != null)` uses the custom implementation of Unity for `UnityEngine.Object` `!=` and `Equals` the null conditional operator `?.` works only on the underlying `object` (Systm.Objetc) => They behave differently and since as mentioned in the dup it is **not really** `null` if you look at the `System.Object` .. it just returns `true` for `== null` since they overwrite this operator for `UnityEngine.Object` – derHugo Nov 24 '21 at 10:30
  • @derHugo, everything you said is true, but would suggest, that i would receive a `MissingReferenceException`. But that is not the case. As stated in the question, the exceptions we're obtained from the CloudDiagnostics. And after buidling a non debugable build unity throws `NullReferenceException`s instead of `MissingReferenceException`s. And this is the answer to my question! I asked why i got a `NullRefereceException`, which i know now. Could you reopen the question so i can link your answer and explain, that this behind the scenes is a `MissingReferenceException`, that isn't shown correctly – T. Grumser Nov 24 '21 at 12:02
  • Just a shot in the dark here, but could it be that your `if (Chest != null && Chest.gameObject == null) ` statement is throwing an exception because its not passing the second check? If `Chest` is actually null then it would pass the first check but since you're trying to grab the `gameObject` component of an object that doesn't exist it throws a `NullReferenceException` – A-Fairooz Nov 25 '21 at 13:01
  • @A-Fairooz, this cannot be the case, since the second statement (`Chest.gameObject == null`) will not be evauated, if the first statement (`Chest != null`) is already false, which would be the case if `Chest` was `null`. – T. Grumser Nov 25 '21 at 15:07

0 Answers0