NullReferenceException: Object reference not set to an instance of an object (117005)

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);
    	}
    }

Did you assign the audio files correctly in the editor?

yes i believe so [32589-untitled.png|32589]

2 Answers

2

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.

i am assigning an audio clips to the inspector i assigned three of them

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.

You could use some reference in your answer, like "As it was said in a deleted comment on this post" or "I will just rephrase a deleted comment".

I don't remember a comment saying what I have said. See that I am not talking about the AudioClips as other people have pointed, I am talking about arrays members and AudioSource components. If there was a comment with such information, I am sorry not to have referenced it, I usually do.

Ok, for some reason my comment said the same but was under an answer thatg got deleted and the com with it....well hopefully will come by and either confirm this fixed it.

@fafase You are right, your comment was in my answer here. But my answer wasn't like to the people therefore I had to delete it.But when the my answer and your comments still was, the answer from @dmg0600 was already written.

thks it work now is there a way i can say that this question have been solve i remember reading that when a question is answer to a satifatory u check something that say this have been solve or something first time using this to get a question answer