FPS picking up items

Hi,

I’ve got some trouble picking up items in FPS. I’ve got a FULLY working inventory system. Only I can’t call the Loot() function:

function Update () 
{ 
   distance = Mathf.Sqrt((playerTransform.position - transform.position).sqrMagnitude); 

   if(Input.GetKeyUp(KeyCode.E) && distance <= 4.0)
   { 
      Debug.Log("Looting!");
      Loot(); 
   } 
} 

I now got this. But this isn’t working. I want to get close to the object, Press E and then call the Loot() function.

Alright, I’ll break it all down and hopefully we can get it to work. Try creating a new variable to hold your fully working inventory system’s script (obviously change the names of variables to whatever you prefer and make sure your GetComponent references your script’s actual name - it’s probably not called FullyWorkingInventorySystemScript, as that’s a silly name).

var fullyWorkingInventorySystemObject : GameObject;

Then call your function like this;

fullyWorkingInventorySystemObject.GetComponent("FullyWorkingInventorySystemScript").Loot();

Drag whatever object you’ve attached your script to onto that exposed variable, and test to see if it now works. Fix any minor errors that the debug log might throw back at you (eg. spelling errors and missing semicolons). Once you’ve done that and tested it, post back here to say if it’s working or not. If not, then we’ll try something else.

It’s working:

function Update () 
{ 
   
   var dist = Vector3.Distance(playerTransform.position, thisTransform.position);
   if(Input.GetKeyUp(KeyCode.E) && dist < 5.0)
   { 
      Debug.Log("Looting!");
      Loot(); 
   } 
}