Shooting on click odd error...

I have a button and when clicked it shoots a sphere.

But even though my script works well I still get an error:

Object Reference not set to instance of Object.

Here is my code:

var windowRect = Rect (200, 150, 100, 100);
var projectile : Rigidbody;
var speed = 20; 

function OnGUI () { 
   windowRect = GUI.Window (0, windowRect, DoMyWindow, "");
}

// Make the contents of the window

function DoMyWindow (windowID : int) { 
   if (GUI.Button (Rect (0,0,100,100), "SHOOT"))
      var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation );   

   instantiatedProjectile.velocity = transform.TransformDirection( Vector3(0, 0, speed ) ); 
   Physics.IgnoreCollision( instantiatedProjectile.collider, transform.root.collider );
   print ("Got a click");
}

Oddly even if I did not set instance of Object it still shoots the correct Prefab.

Please Help. Thanks!

Your `if` statement needs to have all the relevant lines enclosed in brackets. Right now only the first line is being executed when the `if` statement is true, and the rest of the code is always executed, which clearly won't work since `instantiatedProjectile` isn't defined.