Picking up a gun on the ground and deleting that gun on the ground.

NOTE: The correct word to use might be Collect gun, and not Pickup gun.

So I basically have an array to pick up guns and switch between guns. It works well! And I also have the script set up so that when I pickup a new gun, the prefab of the gun gets added to my inventory. The problem is destroying the gun that is on the ground. When I use Destroy, it deletes the prefab. How can make it to just delete the copy on the ground?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUpGun : MonoBehaviour
{
private int counter = 0;
GameObject[ ] gunInv = new GameObject[2];
CharMovement player = new CharMovement();
public int currentGun;

private void Start()
{
}

private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == “Gun”)
{
if (counter < 2)
{
addGunToInventory(other.gameObject);
}
else
{
swap(gameObject.GetComponent().currentGun, other.gameObject);
}
}
else
return;
}

public void addGunToInventory(GameObject gun)
{
print(“hello” + "Picked up " + gun.name);
gunInv[counter] = GameObject.Find(gun.name);
Destroy(gun.gameObject);
gunInv[counter].SetActive(true);
if (counter == 0)
{
gameObject.GetComponent().changeGun(counter);
}
counter++;
print(counter);
}

public void swap(int i, GameObject gun)
{
gunInv*.SetActive(false);*
gunInv = gun;
gunInv*.SetActive(true);*
gameObject.GetComponent().changeGun(i);
}
public GameObject active(int i)
{
currentGun = i;
return gunInv*;*
}
}

So what I can tell form your code (btw try using code tags!) is that you’re setting the gunInv with the gun you’re deleting.

gunInv[counter] = GameObject.Find(gun.name);
Destroy(gun.gameObject);

The GameObject.Find will return the first thing it finds. So you should try getting all the guns and try to determine which one needs to be destroyed.

So when picking up the new gun. Should I create a new GameObject newGun = gun. And then destroy gun.gameObject?

edit: that didn’t really work.

This should get you started on how you can do it.

Yeah… I’ve watched that before. That code looks absolutely ugly. Plus it also assumes the player already has the weapons. My player is not supposed to start with any weapons.

Solved,

gun.transform.SetParent(p.transform);

So, basically I have the idea that I will always transform the gun as a child of the player if he doesn’t have the gun already. IF he does have that gun already, take the ammo from the gun, and then delete the gun that is on the floor. Thank you!