So I am working on a pickup and drop object function on a 2d game.
To summarize the function, The player has a disabled object as a players child and when the player picks up the dropped object, the dropped object gets destroyed and the object that is disabled gets activated. After this process, the player has a choice to drop the object again, which makes the object in the players hand back to disabled and the dropped object which is a prefab gets instantiated from the players hand. Oh and of course the prefab object that is dropped has a destroy function whenever the player presses a key to pick it up.
The problem occurs after the instantiation. The instantiated clone’s destroy method doesn’t work after the first process of pick and drop. The original object works perfectly but at the second attempt which is the pickup and drop function for the clone doesn’t work. To be clear the original dropped object’s destroy function works but the clone prefab object’s destroy function doesn’t work even though they both have the same destroy script attached. The instantiate function(which is the drop function) works fine but because the destroy function (which is the pickup) doesn’t work the player just duplicates the object at the pickup process.
Someone plz save me!!
// this is basically the script of the players object activation
public class object_controller : MonoBehaviour
{
public static bool hasItem;
public GameObject spear;
public GameObject spear_dropped;
bool spear_scanner;
void Update ()
{
pickUp();
drop();
}
void pickUp ()
{
if (spear_scanner == true)
{
spear.SetActive(true);
spear_scanner = false;
hasItem = true;
}
}
}
void drop ()
{
if (Input.GetKeyDown(KeyCode.Q) && hasItem == true)
{
hasItem = false;
if (spear.activeSelf == true)
{
spear.SetActive(false);
Instantiate(cane_dropped, transform.position, Quaternion.identity);
}
}
}
void OnTriggerEnter2D ( Collider2D collision )
{
if (collision.CompareTag("spear"))
{
spear_scanner = true;
}
}
void OnTriggerExit2D ( Collider2D collision )
{
if (collision.CompareTag("spear"))
{
spear_scanner = false;
}
}
}
// and this is the script for the object itself which is the spear
public class dropped_objects : MonoBehaviour
{
public static bool scan;
void Update ()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (scan == true && object_controller.hasItem == false)
{
Destroy(gameObject, 0.01f);
}
}
}
void OnTriggerEnter2D ( Collider2D collision )
{
if (collision.CompareTag("hands"))
{
scan = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("hands"))
{
scan = false;
}
}
}