The question is answered, right answer was accepted
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.
tried this code works perfectly for me.no problem with this code.
I created a new project just in case and put this code there, I get the test messages, but the scale isn't changing. I get 3x more Test messages than Test1 or Test2. I don't get it... :(
Answer by Osvaldon · Mar 19, 2016 at 11:54 AM
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;
}
Follow this Question
Related Questions
Need help with Async 0 Answers
why OnTriggerEnter is not work if with void Update 2 Answers
UI image component problems with Character Creation.... 0 Answers
No input after scene load 1 Answer