I’m relatively new here, so I’m having issues with a mobile game I’m developing, I have items dropping from enemies in the world and I have a pick up code;
private void Update()
{
if (isInRange && !isEmpty && Input.GetKeyDown(itemPickupKeyCode))
{
inventory.AddItem(Instantiate(item));
isEmpty = true;
spriteRenderer.color = emptyColor;
}
}
This code works fine when you press E on the keyboard, the item gets added and it disappears from the world. However, I’m making this for mobile so I am making the pick up button an on screen UI button. So at the end of my script I have;
public void PickUp()
{
// Code for UI pick up button
{
inventory.AddItem(Instantiate(item));
isEmpty = true;
spriteRenderer.color = emptyColor;
}
}
Which is attached to the OnClick() of the UI button. However, the UI button works with adding the item to the inventory but the Instantiated item doesn’t disappear and you can just keep clicking the UI button to continuously add it to the inventory. Why would it disappear when you click E on the keyboard but not the UI button? Am I missing something?
I’d say that Issues specific to your implementation has very less chance of others finding fault
Afterall, You will be the one that knows your code best…
(Unless we have full access to your code, and ready to spend time and if it is some not too unobvious bug. Of course any bug can be analyzed, but depends on how much effort one want to put in)
If “spriteRenderer.color = emptyColor;” is the code that makes it disappear, just make sure that the spriteRenderer is properly assigned (Debug.Log ?!). And is the “emptoColor” a variable you created? Does it have proper value in both cases?
1 Like
First up, this isn’t a Game Design problem. It’s 100% a Scripting issue. (Hah, apparently I wasn’t where I thought I was?)
Second, I don’t believe the error is in the bits of code you’ve posted. It’s in something around them, so you’re going to have to show us more than is here to be able to point you in the right direction.
My first questions are, you say that the second bit of code there is “attached to the OnClick()”, what do you mean by that? Are they in the same script or different scripts?
My next question is, are there any errors in the Console tab when this is misbehaving?
1 Like
This has since been solved, thank anyway. Appreciate you guys.