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

i keep on getting a null reference when ever i play. the game work perfectly but this part of the script keep getting me error if(Thunder != null && Thunder.Count > 0) this is a simple lightning script which i translated from c# to javascript and it work perfectly except for the audio

var MinLightningFlashLength : float = 0.2f;
    var MaxLightningFlashLength : float = 0.8f;
    var MaxLightningFlashes : int = 2;
    var MinFlashTimeDistance : float = 0.3f;
    var MaxFlashTimeDistance : float = 0.6;
    var FlashStartIntensity : float = 1.5f;
    
    private var lastTime = 0;
    private var CurrentFlashLength : float = 0;
    private var NextLightningTime : float = 0;
    private var NextLightningFlashes : int = 0;
    private var NextLightningLength : float = 0;
    private var FlashCooldown : float = 0;
    private var FlashIntensityStep : float = 0;
    
    var Thunder : AudioClip[];
    
    function Start () {
    
    }
    function Awake()
    {
    	GenerateLightning();
    }
    function GenerateLightning()
    {
    	NextLightningTime = Random.Range(MinLightningTimeDistance, MaxLightningTimeDistance) + Time.time;
    	NextLightningLength = Random.Range(MinLightningFlashLength, MaxLightningFlashLength);
    	NextLightningFlashes = Random.Range(1, MaxLightningFlashes + 1);
    }
    function Update () 
    {
    	//lightning is displayed
    	if(light.enabled)
    	{
    		CurrentFlashLength += Time.deltaTime;
    		if(CurrentFlashLength >= NextLightningLength)
    		{
    			// current flash is finished
    			CurrentFlashLength = 0;
    			light.enabled = false;
    			NextLightningFlashes--;
    		
    			//if finished all flashes
    			if(NextLightningFlashes == 0)
    			{
    				GenerateLightning();
    			}
    			else
    			{
    				//wait for next flash
    				FlashCooldown = Random.Range(MinFlashTimeDistance, MaxFlashTimeDistance);
    			}
    		}
    		else
    		{
    			light.intensity -= FlashIntensityStep * Time.deltaTime;
    		}
    	}
    	// no lightning
    	else
    	{
    		// flash cooldown
    		if(FlashCooldown > 0)
    		{
    			FlashCooldown -= Time.deltaTime;
    			if (FlashCooldown <= 0)
    			{
    				StartLightning();
    			}
    			return;
    		}
    		//time for a new lightning
    		if(Time.time >= NextLightningTime)
    		{
    			StartLightning();
    		}
    	}
    }
    function StartLightning()
    {
    	FlashIntensityStep = FlashStartIntensity/ NextLightningLength;
    	light.intensity = FlashStartIntensity;
    	light.enabled = true;
    	if(Thunder != null && Thunder.Count > 0)
    	{
    		 audio.PlayOneShot(Thunder[Random.Range(0,Thunder.Length)]);
    		//audio.PlayOneShot(LightningSounds);
    		//AudioSource.PlayClipAtPoint(LightningSounds[Random.Range(0, LightningSounds.Count)],Camera.main.transform.position);
    	}
    }

Are you actually assigning any audioclips to Thunder in the inspector? That’s the only way that would be returning null, if I’m not mistaken.

There are two things to change.

First, arrays don’t have a Count member, it’s Length what you are looking for. That is why you are getting the NullReferenceException.

Second, you need to add an AudioSource component to your GameObject for it to play sounds. If you don’t want to add an AudioSource and you don’t care about 3D sound settings, you could use the AudioSource.PlayClipAtPoint(AudioClip clip, Vector3 position) function.

if(Thunder != null && Thunder.Length > 0)
{
    audio.PlayOneShot(Thunder[Random.Range(0,Thunder.Length)]);
    //AudioSource.PlayClipAtPoint(Thunder[Random.Range(0, Thunder.Length)],Camera.main.transform.position);
}

With this setted up it works perfectly.