- Home /
Picking up and object and then dropping it again
I am trying to drop the axe at the player's feet.
public class playerController : MonoBehaviour {
public GameObject playerObject;
public GameObject Stone_Axe_db;
public GameObject Stone_Axe;
public bool hasStoneAxe = false;
public bool axeEquipped = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.G))
{
if (axeEquipped)
{
//Create a new Stone_Axe object at the playerObject's position
Stone_Axe_db.SetActive(false);
hasStoneAxe = false;
axeEquipped = false;
}
}
}
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.name == "Stone_Axe" )
{
Stone_Axe_db.SetActive(true);
hasStoneAxe = true;
axeEquipped = true;
//Get rid of the object
}
}
}
I tried changing active from false to true, but I think that means its just the same object going invisible and visible again? I want to delete the object when I pick it up, and then create another instance of it when I drop it. I tried using destroy() and then instantiate() but I get errors.
Can someone please help me with what I need to put where the comments are to make this work? I am totally new to c# and Unity. TIA.
Using SetActive allows you to enable end disable object. You can see how it reacts in the editor while in playmode to get full idea. Though disabling an object makes it, so you cannot access its properties while disabled. It is not the same as making it invisible. Object has all of its properties disabled.
Basically you want to disable Axe that is on the ground when you "Pick it up" and make it appear again when you drop it right?
If so following question is: Is an axe an object which properties never change?
If so you could make a prefab out of it by dragging it to the project folder and save it. Then you just destroy an object in the scene and whenever you drop it you just Instantiate new one from the prefab. If not then you can also use a prefab but you will need to store properties of the objects your character picked up.
Some errors from destroying an object can be easily fixed, so don''t worry so much about that as there is plenty of solutions.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143319im_/https://answers.unity.com/themes/thub/images/avi.jpg)