-1

I'm using this script attached to a GameObject set as trigger and I haven't been able to resolve this NullReferenceException error:

Object reference is not set to an instance of an object. IntExt_Trigger.awake() (At Assets/scripts/IntExt_Trigger.cs)

I have no idea what might be causing this... since as far as I know the awake function is being applied properly, is there something I'm totally missing?

Here's the script.

using System.Collections.Generic;
using UnityEngine;

public class IntExt_Trigger : MonoBehaviour
{
    private AudioSource ACinside, Blizzard_from_INT, Blizzard_from_INT_2, rain;
    private AudioSource AirlockSealOpen_HV, AirlockSealClose_HV;
    private AudioReverbZone Int_Reverb_Zone, Ext_Reverb_Zone;
    private AudioSource ext_wind;
    
    private void Awake()
    {

        ACinside = GameObject.Find("ACinside").GetComponent<AudioSource>();
        Blizzard_from_INT = GameObject.Find("Blizzard_from_INT").GetComponent<AudioSource>();
        Blizzard_from_INT_2 = GameObject.Find("Blizzard_from_INT_2").GetComponent<AudioSource>();

        AirlockSealOpen_HV = GameObject.Find("AirlockSealOpen_HV").GetComponent<AudioSource>();
        AirlockSealClose_HV = GameObject.Find("AirlockSealClose_HV").GetComponent<AudioSource>();

        Int_Reverb_Zone = GameObject.Find("Int_Reverb_Zone").GetComponent<AudioReverbZone>();
        Ext_Reverb_Zone = GameObject.Find("Ext_reverb_Zone").GetComponent<AudioReverbZone>();
        ext_wind = GameObject.Find("ext_wind").GetComponent<AudioSource>();
            
    }
    //this section above defines the variables and functions//

    private void OnTriggerEnter(Collider other)
    {
        // for OnTriggerEnter to run the trigger and the player model objects must have colliders//
        // the folowing actions are executed when an entrance or collision to the trigger is detected//

        Int_Reverb_Zone.enabled = false;

        if (ext_wind.isPlaying == true)
        {
            ACinside.Stop();
            Blizzard_from_INT.Stop();
            rain.Stop();
            

            Int_Reverb_Zone.enabled = false;
            Ext_Reverb_Zone.enabled = true;

            ext_wind.Play();
        }
        else
        {
            ACinside.Play();
            Blizzard_from_INT.Play();
            Blizzard_from_INT_2.Play();
            rain.Play();
           

            Int_Reverb_Zone.enabled = true;
            Ext_Reverb_Zone.enabled = false;

            ext_wind.Stop();
        }
    }

   
}





  • @ken absolutely bad example ^^ `int` is a **value** type and **never** `null`! Also to your example in general note that uninitialized != `null` (but actually worse) .. your first code wouldn't even compile at all ;) – derHugo May 03 '21 at 20:39
  • Basically any of your `Find` calls may fail and return `null` if there is no such named and active object in the scene.. get a proper IDE and [Debug your code with breakpoints](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) also the error message a) tells you exactly in which line it was thrown and b) if you double click it it even brings you exactly to the line of code in the configured IDE ... – derHugo May 03 '21 at 20:42

1 Answers1

-2

you should validate all components you're getting, something like that:

GameObject?.Find("ACinside")?.GetComponent<AudioSource>();

either GameObject or ACinside object is null.

Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
  • 1
    [Do not use the Null coalescing operator for `UnityEngine.Object`!](https://stackoverflow.com/questions/56875706/how-am-i-misusing-the-null-coalescing-operator-is-this-evaluating-null-correc) – derHugo May 03 '21 at 20:36
  • 1
    Also note that `GameObject` is a type and `GameObject.Find` a static method ... `GameObject?.Find` makes no sense at all ;) – derHugo May 04 '21 at 04:50