Help with random.range

this code started as me trying to help someone else in Unity answers. I have been at it for a while but I’m getting NullReferenceException :Object reference not set to an instance of an object(IConvertible convertible) in line 23. The script makes object shoot every .5 secs and is supposed to cycle through integers in an array every 2 seconds. It should work but doesn’t.

var projectile : Rigidbody; //The Bullet
var fireRate : float = .5;//Time Delay Between Shots!
var shootTime: float = 0.3;
//RandomizedShotSpeed//
var switchSpeed : float = 2.0;
var index : float = 100;

function Update() 
{
	var speed = Array(50,100,150,200,250);
		
	if(Time.time >= switchSpeed )
	{
		index = Random.Range(0, speed.length);
		switchSpeed += 2.0;
		var currentSpeed = speed[index];
    	print(currentSpeed);
    }
    
	if(Time.time >= shootTime)
	{
    	var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
      	instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, currentSpeed ) );
    	shootTime += fireRate;		
	}	
}

I got it working!!! I actually had it working before but I control + z and accidently hit another key so I couldn’t go forward back to the working script, and now it cost me like an hour and a half seriously. And yea all you guys were right and I had already tried that but the problem is that I had entered var currenSpeed without the “t” and didn’t catch it. Notice how I had index=100 in the original post which of course makes no sense. That used to be currenSpeed without the t then after an undo u guys got that code. Anyway here is the correct code.

var projectile : Rigidbody; //The Bullet
var fireRate : float = .5;//Time Delay Between Shots!
var shootTime: float = 0.3;
//RandomizedShotSpeed//
var switchSpeed : float = 2.0;
var currentSpeed : float = 100;

function Update() 
{
	var speed = Array(50,100,150,200,250);
		
	if(Time.time >= switchSpeed )
	{
		var index = Random.Range(0, speed.length);
		switchSpeed += 2.0;
		currentSpeed = speed[index];
    	print(currentSpeed);
    }
    
	if(Time.time >= shootTime)
	{
    	var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
      	instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, currentSpeed ) );
    	shootTime += fireRate;		
	}	
}

Your problem is declaring variables within an If statement. It doesn’t exist the first time the Update() function gets called.

 var projectile : Rigidbody; //The Bullet
    var fireRate : float = .5;//Time Delay Between Shots!
    var shootTime: float = 0.3;
    //RandomizedShotSpeed//
    var switchSpeed : float = 2.0;
    var index : float = 100;
     
    var currentSpeed = 50;
     
    function Update() 
    {
        var speed = Array(50,100,150,200,250);
     
        if(Time.time >= switchSpeed )
        {
           	index = Random.Range(0, speed.length);
          	switchSpeed += 2.0;
        	currentSpeed = speed[index];
        }
     
        if(Time.time >= shootTime)
        {
            var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
            instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, currentSpeed ) );
            shootTime += fireRate;     
        }  
    }