Please help my with this stupid script D:

I have a problem whit this script for pick up ammo and add the bullets in the gun(M4A1).
the error is
----NullReferenceException----
UnityEngine.Component.get_gameObject () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:170)
UnityEngine.Component.GetComponent (System.String type) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:197)
TakeAmmo.AddClip () (at Assets/ScriptsDB/AIScripts/TakeAmmo.js:56)
TakeAmmo.Update () (at Assets/ScriptsDB/AIScripts/TakeAmmo.js:30) HELP ME PLEASE

var obj1 : Transform; //the gameobject (Clip)

var obj2 : Transform; //my Player

var Bar : float; 	

private var ammoBox : Transform;

private var M4A1 : Transform;
  
function Start () 

{	
    var Bar = (Screen.width / 1);
}


function Update()
 
{
    var dist = Vector3.Distance( obj1.transform.position,obj2.transform.position );
    											//if distance is < 4
    if(dist < 4)
    {
    
           if(Input.GetKeyDown ("f")) //if I press f
       
            {
                AddClip();                	  		//Go to function AddClip
            }

    }
    
}

           
function OnGUI() //function for Gui		

{   

    var dist = Vector3.Distance( obj1.transform.position,obj2.transform.position );
    
    if(dist < 4)

    {      
        GUI.Box (new Rect(10, 740, Bar, 40), "Ammo for M4A1, F to pickup");
            
    }       
   
}

function AddClip ()
{	
	//HERE IS THE PROBLEM!!!!
	var Ammo = ammoBox.GetComponent("AmmoBox"); 	//get script from ammoBox		
	
	var Addammo = M4A1.GetComponent("gunscript");           //								

    	if (Addammo)                 				// Add Clip to to the var clips in the script "gunscript"

	if (Ammo)						//

        Addammo.clips += Ammo.ammo;				//
	
	
	
	DestroyAmmo(); //after addedclip destroy the gameobject ammo
	
}

function DestroyAmmo()
{
	Destroy(gameObject); 
}

//and this is the variable clips in the script "gunscript"

//var clips : int = 20;

//the variable clips are the clips in the M4A1 for reloading


//this is the variable ammo in the script "AmmoBox"

//var ammo : int = 60;

You are trying to get a component that’s not there yet. The player has to get close and press f, yet you are running this function without the player even getting close, and are wasting valuable resources.

use a trigger on the item

// use this, you need a collider set as a trigger and rigidbody attached.
//you might need another collider as rigidbody will make it fall through floor
function OnTriggerEnter(other : Collider)
{
      if(other.CompareTag("Player"))
            AddClip();
}

Sure but when I try to collide with the clip and try to press F but does not work. There is something wrong with the OnTriggerEnter.
P.S. I already tried putting in both obj (1 and 2) the rigidbody. I don’t have errors in console.
I applied this script to my player.
The script:

[/CODE]var obj1 : Transform; //the gameobject (Clip)

var obj2 : Transform; //my Player

var Bar : float;

var Amount : int = 60;

function Start ()
{
var Bar = (Screen.width / 1);

other = obj1;
}

function Update()
{

}

function OnTriggerEnter(other : Collider)

{

if(other.CompareTag(“Ammo”))

TakeClip();

}

function TakeClip()
{
if(Input.GetKeyDown (“f”))
{
AddClip();
}
}

function AddClip()
{
var script : gunscript = GetComponent(gunscript);

script.clips += Amount;

DestroyClip();
}

function DestroyClip()
{
Destroy(obj1);
}

function OnGUI() //function for Gui

{

var dist = Vector3.Distance(obj1.position,obj2.position);

if(dist < 2)

{
GUI.Box (new Rect(10, 740, Bar, 40), “Ammo for M4A1, F to pickup”);

}

}[/CODE]

Anyone??

change OnTriggerEnter to OnTriggerStay

Yea. It Work. But i get the NullReferenceException: Object reference not set to an instance of an object
TakeAmmo.AddClip () (at Assets/ScriptsDB/AIScripts/TakeAmmo.js:44)
TakeAmmo.TakeClip () (at Assets/ScriptsDB/AIScripts/TakeAmmo.js:36)
TakeAmmo.OnTriggerStay (UnityEngine.Collider other) (at Assets/ScriptsDB/AIScripts/TakeAmmo.js:28)
and the addclip to in M4A1 don’t added.

Your ammoBox and M4A1 variables are private but you never set them to anything in your script (if they were public then one would assume you’re assigning via Inspector).