Error: NullReferenceException

Hello sorry I am new to unity and having trouble instantiating a camera on button down and turning my main off then having the “newCamera” follow my “newAmmo” projectile until its destroyed or the “cameraChange” button goes up Here is the code I tried again sorry I am new and don’t know all the posting rules either

#pragma strict

static var canThrow : boolean = true;
var throwSound : AudioClip;
var ammoObject : Rigidbody;
var throwForce : float;
var arc : float;
private var changeCamera : boolean;
var ammoCamera : Camera;
function Start () 
{


}

function Update () 
{

if(Input.GetButtonUp("Fire1")  canThrow)
{
//Script for creating Ammo projectile on mouse click down 
//Works good
audio.PlayOneShot(throwSound);
var newAmmo : Rigidbody = Instantiate(ammoObject,transform.position, transform.rotation);
newAmmo.name = "Ammo";



newAmmo.transform.Rotate(0,90,-5);
newAmmo.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, throwForce));
Physics.IgnoreCollision(transform.root.collider,ne wAmmo.collider, true);

}
// Change camera to Ammo only on true conditions
// changeCamera "F" button down and Ammo object is instantiated
if(Input.GetButtonDown("changeCamera")  GameObject.Find("Ammo") != null)
{
var newCamera = Instantiate(ammoCamera,transform.position, transform.rotation);
newCamera.name ="ammoCamera";

Camera.main.enabled = false;
newCamera.enabled= true;


newCamera.transform.parent = newAmmo.transform;

}

// Change Camera Back only on true conditions
// changeCamera "F" button up and Ammo object is instantiated
if(Input.GetButtonUp("changeCamera")  GameObject.Find("Ammo") != null)
{
newCamera.enabled= false;
Camera.main.enabled = true;
}


}
@script RequireComponent(AudioSource)

I get an error only when changeCamrea button is pressed and the error is on line
newCamera.transform.parent = newAmmo.transform;

I have a camera prefab named ammoCamera dragged and loaded I also see the ammoCamera being created in the hiearchy but still get a null reference exception I am lost again sorry I am new and don’t know all the rules

Your newAmmo variable is named in a “if” clause and will only be useful in that clause. You need to name it at the top, outside of brackets so it can be seen throughout the script, or you can name it at the top of the update function if only used in an update, or bring the call for it into the if clause.

Thanks that does make sense but I am having trouble getting the solution to work for me. Can I get a working example please. I keep getting unexpected token error or transform is not a member of object error

Just declare variable newAmmo at the start of the code. Don’t declare it in IF loop, only assign it the value there.