Search Unity

Bug NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by Magic-Goupil, Mar 29, 2023.

  1. Magic-Goupil

    Magic-Goupil

    Joined:
    Mar 19, 2020
    Posts:
    8
    [I've put in all the code but don't worry, the error says that the problem is at line 309, which calls a function that is at line 79. The function is only 5 lines long. I put all the code to help understanding if needed.]

    Hello,

    As often, I come across this "Null Reference Exception" error.
    I usually manage to find the instance that is called incorrectly, but in this case I'm a bit stuck so I'm asking for your help.

    The error occurs dozens to hundreds of times per second, causing the application to freeze at times.

    (Small precision: It is a script of the Oculus asset. From the GameObject "HandsInteractorLeft" or "HandsInteractorRight", in the script "InteractorsGroup".
    The GameObject "HandsInteractor [Right or Left]" allows you to interact with elements, such as pressing a button for example.)

    Here is the code, (the error is lower, but is located at line 79) :​

    Code (CSharp):
    1. /************************************************************************************
    2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
    3.  
    4. Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
    5. https://developer.oculus.com/licenses/oculussdk/
    6.  
    7. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
    8. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
    9. ANY KIND, either express or implied. See the License for the specific language governing
    10. permissions and limitations under the License.
    11. ************************************************************************************/
    12.  
    13. using System;
    14. using System.Collections.Generic;
    15. using UnityEngine;
    16. using UnityEngine.Assertions;
    17. using UnityEngine.Serialization;
    18.  
    19. namespace Oculus.Interaction
    20. {
    21.     /// <summary>
    22.     /// InteractorGroup coordinates between a set of Interactors to
    23.     /// determine which Interactor(s) should be enabled at a time.
    24.     ///
    25.     /// By default, Interactors are prioritized in list order (first = highest priority).
    26.     /// Interactors can also be prioritized with an optional ICandidateComparer
    27.     /// </summary>
    28.     public class InteractorGroup : MonoBehaviour, IInteractor
    29.     {
    30.         [SerializeField, Interface(typeof(IInteractor))]
    31.         private List<MonoBehaviour> _interactors;
    32.  
    33.         protected List<IInteractor> Interactors;
    34.  
    35.         public bool IsRootDriver { get; set; } = true;
    36.  
    37.         private IInteractor _candidateInteractor = null;
    38.         private IInteractor _activeInteractor = null;
    39.  
    40.         [SerializeField, Interface(typeof(ICandidateComparer)), Optional]
    41.         private MonoBehaviour _interactorComparer;
    42.  
    43.         public int MaxIterationsPerFrame = 3;
    44.         protected ICandidateComparer CandidateComparer = null;
    45.  
    46.         public event Action<InteractorStateChangeArgs> WhenStateChanged = delegate { };
    47.         public event Action WhenPreprocessed = delegate { };
    48.         public event Action WhenProcessed = delegate { };
    49.         public event Action WhenPostprocessed = delegate { };
    50.  
    51.         protected virtual void Awake()
    52.         {
    53.             Interactors = _interactors.ConvertAll(mono => mono as IInteractor);
    54.             CandidateComparer = _interactorComparer as ICandidateComparer;
    55.         }
    56.  
    57.         protected virtual void Start()
    58.         {
    59.             foreach (IInteractor interactor in Interactors)
    60.             {
    61.                 Assert.IsNotNull(interactor);
    62.             }
    63.  
    64.             foreach (IInteractor interactor in Interactors)
    65.             {
    66.                 interactor.IsRootDriver = false;
    67.             }
    68.  
    69.             if (_interactorComparer != null)
    70.             {
    71.                 Assert.IsNotNull(CandidateComparer);
    72.             }
    73.         }
    74.  
    75.         public void Preprocess()
    76.         {
    77.  
    78.             foreach (IInteractor interactor in Interactors)
    79.             {
    80.            
    81.                 interactor.Preprocess();
    82.             }
    83.             WhenPreprocessed();
    84.         }
    85.  
    86.         public void Process()
    87.         {
    88.             if (_activeInteractor != null)
    89.             {
    90.                 _activeInteractor.Process();
    91.             }
    92.             WhenProcessed();
    93.         }
    94.  
    95.         public void Postprocess()
    96.         {
    97.             foreach (IInteractor interactor in Interactors)
    98.             {
    99.                 interactor.Postprocess();
    100.             }
    101.  
    102.             if (_activeInteractor != null && _activeInteractor.State == InteractorState.Disabled)
    103.             {
    104.                 _activeInteractor = null;
    105.             }
    106.  
    107.             WhenPostprocessed();
    108.         }
    109.  
    110.         public void ProcessCandidate()
    111.         {
    112.             _candidateInteractor = null;
    113.  
    114.             foreach (IInteractor interactor in Interactors)
    115.             {
    116.                 interactor.ProcessCandidate();
    117.  
    118.                 if (interactor.HasCandidate)
    119.                 {
    120.                     if (_candidateInteractor == null)
    121.                     {
    122.                         _candidateInteractor = interactor;
    123.                     }
    124.                     else if (Compare(_candidateInteractor, interactor) > 0)
    125.                     {
    126.                         _candidateInteractor = interactor;
    127.                     }
    128.                 }
    129.             }
    130.  
    131.             if (_candidateInteractor == null && Interactors.Count > 0)
    132.             {
    133.                 _candidateInteractor = Interactors[Interactors.Count - 1];
    134.             }
    135.         }
    136.  
    137.         public void Enable()
    138.         {
    139.             if (_activeInteractor == null)
    140.             {
    141.                 return;
    142.             }
    143.             _activeInteractor.Enable();
    144.         }
    145.  
    146.         public void Disable()
    147.         {
    148.             foreach (IInteractor interactor in Interactors)
    149.             {
    150.                 interactor.Disable();
    151.             }
    152.  
    153.             State = InteractorState.Disabled;
    154.         }
    155.  
    156.         public void Hover()
    157.         {
    158.             if (State != InteractorState.Normal)
    159.             {
    160.                 return;
    161.             }
    162.  
    163.             _activeInteractor = _candidateInteractor;
    164.             _activeInteractor.Hover();
    165.             State = InteractorState.Hover;
    166.         }
    167.  
    168.         public void Unhover()
    169.         {
    170.             if (State != InteractorState.Hover)
    171.             {
    172.                 return;
    173.             }
    174.  
    175.             if (_activeInteractor != null)
    176.             {
    177.                 _activeInteractor.Unhover();
    178.             }
    179.  
    180.             _activeInteractor = null;
    181.  
    182.             State = InteractorState.Normal;
    183.         }
    184.  
    185.         public void Select()
    186.         {
    187.             if (State != InteractorState.Hover)
    188.             {
    189.                 return;
    190.             }
    191.  
    192.             _activeInteractor.Select();
    193.             State = InteractorState.Select;
    194.         }
    195.  
    196.         public void Unselect()
    197.         {
    198.             if (State != InteractorState.Select)
    199.             {
    200.                 return;
    201.             }
    202.  
    203.             if (_activeInteractor != null)
    204.             {
    205.                 _activeInteractor.Unselect();
    206.             }
    207.  
    208.             State = InteractorState.Hover;
    209.         }
    210.  
    211.         public bool ShouldHover => _activeInteractor != null && _activeInteractor.ShouldHover;
    212.  
    213.         public bool ShouldUnhover => _activeInteractor == null || _activeInteractor.ShouldUnhover;
    214.         public bool ShouldSelect => _activeInteractor != null && _activeInteractor.ShouldSelect;
    215.  
    216.         public bool ShouldUnselect => _activeInteractor == null || _activeInteractor.ShouldUnselect;
    217.  
    218.         private void DisableAllInteractorsExcept(IInteractor enabledInteractor)
    219.         {
    220.             foreach (IInteractor interactor in Interactors)
    221.             {
    222.                 if (interactor == enabledInteractor) continue;
    223.                 interactor.Disable();
    224.             }
    225.         }
    226.  
    227.         public int Identifier => _activeInteractor != null
    228.             ? _activeInteractor.Identifier
    229.             : Interactors[Interactors.Count - 1].Identifier;
    230.  
    231.         public bool HasCandidate => _candidateInteractor != null && _candidateInteractor.HasCandidate;
    232.  
    233.         public object Candidate => HasCandidate ? _candidateInteractor.Candidate : null;
    234.  
    235.         public bool HasInteractable => _activeInteractor != null &&
    236.                                        _activeInteractor.HasInteractable;
    237.  
    238.         public bool HasSelectedInteractable => State == InteractorState.Select &&
    239.                                                _activeInteractor.HasSelectedInteractable;
    240.  
    241.         private InteractorState _state = InteractorState.Normal;
    242.  
    243.         public InteractorState State
    244.         {
    245.             get
    246.             {
    247.                 return _state;
    248.             }
    249.             private set
    250.             {
    251.                 if (_state == value)
    252.                 {
    253.                     return;
    254.                 }
    255.                 InteractorState previousState = _state;
    256.                 _state = value;
    257.  
    258.                 WhenStateChanged(new InteractorStateChangeArgs
    259.                 {
    260.                     PreviousState = previousState,
    261.                     NewState = _state
    262.                 });
    263.             }
    264.         }
    265.  
    266.         public virtual void AddInteractor(IInteractor interactor)
    267.         {
    268.             Interactors.Add(interactor);
    269.             _interactors.Add(interactor as MonoBehaviour);
    270.             interactor.IsRootDriver = false;
    271.         }
    272.  
    273.         public virtual void RemoveInteractor(IInteractor interactor)
    274.         {
    275.             if (!Interactors.Remove(interactor))
    276.             {
    277.                 return;
    278.             }
    279.             _interactors.Remove(interactor as MonoBehaviour);
    280.             interactor.IsRootDriver = true;
    281.         }
    282.  
    283.         private int Compare(IInteractor a, IInteractor b)
    284.         {
    285.             if (!a.HasCandidate && !b.HasCandidate)
    286.             {
    287.                 return -1;
    288.             }
    289.  
    290.             if (a.HasCandidate && b.HasCandidate)
    291.             {
    292.                 if (CandidateComparer == null)
    293.                 {
    294.                     return -1;
    295.                 }
    296.  
    297.                 int result = CandidateComparer.Compare(a.Candidate, b.Candidate);
    298.                 return result > 0 ? 1 : -1;
    299.             }
    300.  
    301.             return a.HasCandidate ? -1 : 1;
    302.         }
    303.  
    304.         protected virtual void Update()
    305.         {
    306.             if (!IsRootDriver)
    307.             {
    308.                 return;
    309.             }
    310.  
    311.             Preprocess();
    312.  
    313.             InteractorState previousState = State;
    314.             for (int i = 0; i < MaxIterationsPerFrame; i++)
    315.             {
    316.                 if (State == InteractorState.Normal ||
    317.                     (State == InteractorState.Hover && previousState != InteractorState.Normal))
    318.                 {
    319.                     ProcessCandidate();
    320.                 }
    321.  
    322.                 previousState = State;
    323.                 Process();
    324.  
    325.                 if (State == InteractorState.Disabled)
    326.                 {
    327.                     break;
    328.                 }
    329.  
    330.                 if (State == InteractorState.Normal)
    331.                 {
    332.                     if (_candidateInteractor != null && _activeInteractor != _candidateInteractor)
    333.                     {
    334.                         _activeInteractor = _candidateInteractor;
    335.                         Enable();
    336.                         DisableAllInteractorsExcept(_activeInteractor);
    337.                     }
    338.  
    339.                     if (ShouldHover)
    340.                     {
    341.                         Hover();
    342.                         continue;
    343.                     }
    344.                     break;
    345.                 }
    346.  
    347.                 if (State == InteractorState.Hover)
    348.                 {
    349.                     if (ShouldSelect)
    350.                     {
    351.                         Select();
    352.                         continue;
    353.                     }
    354.                     if (ShouldUnhover)
    355.                     {
    356.                         Unhover();
    357.                         continue;
    358.                     }
    359.                     break;
    360.                 }
    361.  
    362.                 if(State == InteractorState.Select)
    363.                 {
    364.                     if (ShouldUnselect)
    365.                     {
    366.                         Unselect();
    367.                         continue;
    368.                     }
    369.                     break;
    370.                 }
    371.             }
    372.  
    373.             Postprocess();
    374.         }
    375.  
    376.         #region Inject
    377.  
    378.         public void InjectAllInteractorGroup(List<IInteractor> interactors)
    379.         {
    380.             InjectInteractors(interactors);
    381.         }
    382.  
    383.         public void InjectInteractors(List<IInteractor> interactors)
    384.         {
    385.             Interactors = interactors;
    386.             _interactors = interactors.ConvertAll(interactor => interactor as MonoBehaviour);
    387.         }
    388.  
    389.         public void InjectOptionalInteractorComparer(ICandidateComparer comparer)
    390.         {
    391.             CandidateComparer = comparer;
    392.             _interactorComparer = comparer as MonoBehaviour;
    393.         }
    394.  
    395.         #endregion
    396.     }
    397. }
    398.  

    This is my error :

    NullReferenceException: Object reference not set to an instance of an object
    Oculus.Interaction.InteractorGroup.Preprocess () (at D:/UNITY/UNITYPROJECT/FormationOculusNewTeleportNEW/Assets/Oculus/Interaction/Runtime/Scripts/Interaction/Core/InteractorGroup.cs:79)
    Oculus.Interaction.InteractorGroup.Update () (at D:/UNITY/UNITYPROJECT/FormationOculusNewTeleportNEW/Assets/Oculus/Interaction/Runtime/Scripts/Interaction/Core/InteractorGroup.cs:309)


    Thanks in advance for your futur reply,
    Sincerely.
     
    Last edited: Mar 29, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    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 <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    If it is appearing in third party code, check over all the requirements for using that code. Most likely you failed to initialize or provide something that is expected. Start from the docs for this package, or else from example code.
     
  3. Magic-Goupil

    Magic-Goupil

    Joined:
    Mar 19, 2020
    Posts:
    8
    First of all, thank you for your answer.

    I am well aware of the methods that need to be done to solve this error, and it is not so complicated in itself when it comes to OUR code.

    But solving this problem in an asset that has dependencies, and links to other scripts, all of which are at least 500 lines long, I feel like it's not the same thing...

    That's why I'm asking for help. Maybe someone has already succeeded? Maybe someone more experienced than me can find the solution easily?