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;