Code doesn't work inside void Update()

void Update()
{
if (Input.GetMouseButton(0))
{
Debug.Log(“Test”);
if (gameObject.name == “Decrease”)
{
Debug.Log(“Test1”);
bar = GameObject.Find(“Bar”);
bar.transform.localScale += new Vector3(-0.01875F, 0, 0);
}
else if (gameObject.name == “Increase”)
{
Debug.Log(“Test2”);
bar = GameObject.Find(“Bar”);
bar.transform.localScale += new Vector3(0.01875F, 0, 0);
}
}
}

The code works fine under OnMouseDown, but when I put it under Update, the scale is increased once no matter where on screen I click and nothing else seems to work anymore except for the test messages. And for some reason I get 8 messages every frame for Test message (1 for other messages), though I think there should be only 1 per frame.

Okay, it just hit me that void Update() doesn’t give me a gameObject, so the fix was replacing gameObject with a new GameObject variable clickedObject and then getting it with OnMouseDown.

private GameObject clickedObject;

void OnMouseDown()
{
    clickedObject = gameObject;
}

void OnMouseUp()
{
    clickedObject = null;
}