Changing the Variable of an objects script, through a different objects event.

Hey guys, this is my first post on Unity Answers!

I’m having a little bit a trouble, on something I’m sure should be simple.

I have an Object (ObtainableObject) that should be picked up by the player, and then removed from the game world.
When the player move’s into it’s range, they are prompted to push the “pick up object” key, which will add +1 to the players Inventory Script value of “PickupObjectsHeld”…
However, I’m having trouble figuring out exactly how to reach that result…
I’v looked through several help threads, and I’v gotten closer to the answer, but not quite there yet…
Here’s my code for the Obtainable Object Class!

using UnityEngine;

using System.Collections;

public class ObtainableObjectScript : MonoBehaviour {

// Call from the Hierarchy
public GameObject thePlayer;
public Vector3 DistanceToPlayer;
// Use this for initialization
void Start () 


// Update is called once per frame
public void Update()
	GameObject thePlayer = GameObject.Find("ThePlayer");
	DistanceToPlayer = thePlayer.transform.position - transform.position;
	if (DistanceToPlayer.magnitude < 10)
		print("Pick Up");
		if (Input.GetButtonDown("Use/Pickup"))
			gameObject.Find("ThePlayer").GetComponent("ThePlayerInventory").pickupObjectsHeld+= 1;


If your obtainable object is something you would bump into to collect, you could simply add a collider/trigger to it, and OnTriggerEnter (or collider if thats what you’re doing), do something like so:

ObtainableObjectScript otherObject = collisionInfo.collider.transform.GetComponent<ObtainableObjectScript>();

that will give you a reference to the object that you touched, and from there you can do whatever you want with it.

for further reading: