Why does this part of code run twice?
I am making a basic inventory with stacking and for some reason whenever i click on the item it subtracts 2 instead of just 1. Can anyone help me? Thank you
void OnGUI ()
{
GUI.DrawTexture(windowRect, inventoryBackgroundTexture);
for (int x=0; x<across; x++)
{
for (int y=0; y<down; y++)
{
Rect buttonRect = new Rect(_offset*2+buttonSize*x, _offset*2.5f+buttonSize*y, buttonSize, buttonSize);
if (inventory[x, y] != null)
{
if (inventory[x, y].itemCount > 0)
{
GUI.DrawTexture(buttonRect, inventory[x, y].texture);
Vector2 mousePos = Input.mousePosition;
mousePos.y = Screen.height-Input.mousePosition.y;
if (buttonRect.Contains(mousePos))
{
if (Input.GetButtonDown("Fire1"))
{
SubtractItem(x, y); *** This part runs 2 times ***
}
GUI.DrawTexture(buttonRect, hoverTexture);
}
if (inventory[x, y] != null)
{
GUI.Label(buttonRect, inventory[x, y].itemCount.ToString(), itemCountStyle);
}
}
}else{
GUI.DrawTexture(buttonRect, blankTexture);
}
}
}
}
$$anonymous$$ight have the script on the game object twice :/ Also where is the SubtractItem(); ?
Answer by Aranda · Nov 11, 2015 at 01:23 PM
Probably you have two cameras with Gui Layer on them? You should move any input handling out into the Update() function.
There is only 1 game object with a single script on it. I have no clue why it runs twice. I'll try moving the input to the update function but I don't think that will help my problem.
Your answer
Follow this Question
Related Questions
Question about protected and abstract classes. 1 Answer
Void being called 4 times, no apparent reason 1 Answer
List check argument of item 3 Answers
[Help] I don't know how to create a stacking system for my inventory! 3 Answers
Nested coroutines 0 Answers