show object when player keypress on another object

hey guys, im working on a FPS and i need help :slight_smile:

trying to make it so, when i walk upto a object and press E that object goes away and spawns another object somewhere else, but have it so the items holding the script as a inspector so you can drag over the object you want it to spawn from that stage.

its for a objective which is find items one by one, etc. any help would be great :slight_smile:

thanks guys

For the Input you can do this in the update function of the script that takes care of this functionality

function Update () {
		if (Input.GetKeyDown ("e"))
			// the key was pressed

For finding what objects are near, you can use the Physics.overlaySpehere, which essentially check using a sphere which objects are at a certain radius. You can then check if the object has a particular tag, to for instance destroy it, and then create the other object somewhere else

Collider[] cols = Physics.OverlapSphere(transform.position, 2.0f);
    foreach (Collider hit in cols) {
         if (hit && hit.tag == "ObjectTag"){

To create the other object somewhere else, you can instantiate a prefab that you have under the resources folder of your assets.

public GameObject theGameObjectToInstantiate
void CreateNewObjectSomewhereElse(){
   Instantiate(theGameObjectToInstantiate, transform.position, transform.rotation);

You will want to replace the transform.position for the location of the place where you want to instantiate it