- Home /
Item pickup script not working as intended,Item pickup action happens when button isn't pressed
Hi I'm trying to make a pickup item mechanic (inventory system eventually) but I have encountered an issue. I used OnTriggerEnter and OnTriggerExit to set the boolean of whether the player is in pickupRange of the object and if TRUE, the HUD will display its "Pick up item" message else its not active. This has worked, at least for the HUD message which I believe means the pickupRange bool is working properly so next I tried to add a pickupItem action where when you press E you pickup the item.
I went about this in 2 ways which I labelled --1 and --2 as comments:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class itemPickup : MonoBehaviour
{
// Which MessagePanel to activate/deactivate
public GameObject pickupMessage;
//Determines whether PLAYER is colliding with object
private bool pickupRange = false;
KeyCode pickupKey = KeyCode.E;
void Update()
{
//If PICKUPRANGE is true, show message
if (pickupRange)
{
pickupMessage.SetActive(true);
//--1 Result happens when collision with item, not when button pressed
//if (Input.GetKeyDown(pickupKey)) ;
//{
// print("Picked up!");
// Destroy(gameObject);
//}
}
//If PICKUPRANGE is false, hide message
else
{
pickupMessage.SetActive(false);
}
//--2 Result happens when game starts, not when button pressed or even when colliding
if (pickupRange && Input.GetKeyDown(pickupKey)) ;
{
print("Picked up!");
Destroy(gameObject);
}
}
void OnTriggerEnter(Collider collider)
{
//When PLAYER collides with ITEM, set PICKUPRANGE to true
if (collider.gameObject.tag == "Player") ;
{
pickupRange = true;
print("True");
}
}
void OnTriggerExit(Collider collider)
{
//When PLAYER leaves collision with ITEM, set PICKUPRANGE to false
if (collider.gameObject.tag == "Player") ;
{
pickupRange = false;
print("False");
}
}
}
From my understanding (I may be wrong because I have never coded before), they should work as for 1, the E button should only do something if the pickupRange bool is true while for 2, the game checks for both if pickupRange bool is true and if E is pressed.
Either way I would have thought that they should have the same result but when I ran the test they both didn't work as expected:
1 - When player collision box enters the item's, the item gets destroyed (as intended) without the E button being pressed. And also the HUD message stayed up which I would assume is because there was no OnTriggerExit to make the pickupRange bool become false.
2 - When I clicked PLAY, the item instantly gets destroyed without the player collision box even colliding with the item's.
Would appreciate it if someone could let me know where I made a mistake and why it's wrong and what's the proper way to go about it and if possible, why. Would love to understand more about the code rather than just copying the codes and be done with it - I've been following tutorials on Youtube but most of them don't explain why something should be done in a certain way so I just end up following without understanding the code fully.
Answer by diego-giacomelli · Nov 04, 2020 at 11:24 AM
You have an ;
left at the end of the line 39
:
if (pickupRange && Input.GetKeyDown (pickupKey));
That ;
left over will cause the command below, inside the {...}
, to always be executed.
This is why you see the item instantly gets destroyed when you clicked the Play button.
UPDATED: Now that I've noticed that you have other if
statements with ;
at the end. Just remove the ;
.
Thank you, that explains everything! Removed the ;
and it's working for the item.
However now I've come across another problem when I made a second item and used the same script. The interaction works as intended except that the message doesn't show for the second object until the first object has been destroyed - note that pressing E when player is within pickupRange still destroys the object, just that the message doesn't show.
In short: Item 1 - Works as intended; Item 2 - $$anonymous$$essage doesn't show if Item 1 is still in the scene, destroying item with E still works normally.
When I removed Item 1 before playing, the script works fine for Item 2 so I think it may be something may be linking the message display to Item 1?
Your answer
![](https://koobas.hobune.stream/wayback/20220613015112im_/https://answers.unity.com/themes/thub/images/avi.jpg)