Button not working (644346)

So, I’m making an idle game to familiarise myself with unity, and I can’t get the code to work for the button increasing “money” (named chaos in the game)

These are screenshots of my code and setup:

Screenshot by Lightshot (ignore the orange buttons, just got them there ready, it’s the purple button and text that matter)
Screenshot by Lightshot (button)
Screenshot by Lightshot (text)

really got no clue what i’m doing wrong, no errors show up for code, debugging found nothing..I’m just confused

Hmm, it all looks right to me, though it’s hard to tell clicking back and forth between all those external screenshots — next time please use the Image and Code buttons in the toolbar to paste your code and images right in your message.

So the next step is to put a Debug.Log in your Button.clicked event (oh, also, it’s very confusing to name your own class the same name as a Unity framework class… it’ll work thanks to namespaces, as long as you’re careful with your using statements, but it’s still confusing). So, yeah, put a Debug.Log in there and see if your message appears in the Console when you click the button.

If it does, then you know your code is running. Check your chaos value with the inspector and see if it’s going up, then put another Debug.Log in the Update method to figure out why it’s not updating the text.

If your Debug.Log doesn’t appear, then your button OnClick event is somehow not wired up correctly.

Also consider the possibility that you may have your Button script installed on more than one object in the scene, which is both confusing you and overwriting the text (since only one of them is being updated). Check over all your objects and make sure your own Button script (as opposed to Unity’s) is only found in one place.

There is no event system in your hierarchy. Without an event system none of the UI will work.

2 Likes

Ow right, thanks BoredMormon, now it works.

Well spotted!

And dangit, in such situations I often recommend selecting the EventSystem and watching it in the inspector as you click on things (a fair amount of detail on “what happened to my event?” appears there). Pursuing that line would have led to the answer right away. But I missed it, and you got it fair and square! :slight_smile:

I have a YouTube channel that focuses on UI techniques. 99% of the comments can be answered with ‘Do you have an event system?’. It’s a surprisingly easy trap to fall into.

1 Like