Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Button not working

Discussion in 'Getting Started' started by KingsRoyalGames, Oct 28, 2016.

  1. KingsRoyalGames

    KingsRoyalGames

    Joined:
    Oct 28, 2016
    Posts:
    2
    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:
    http://prntscr.com/d04ekn
    http://prntscr.com/d04eym (ignore the orange buttons, just got them there ready, it's the purple button and text that matter)
    http://prntscr.com/d04fh6 (button)
    http://prntscr.com/d04fnm (text)

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

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is no event system in your hierarchy. Without an event system none of the UI will work.
     
    JoeStrout and KingsRoyalGames like this.
  4. KingsRoyalGames

    KingsRoyalGames

    Joined:
    Oct 28, 2016
    Posts:
    2
    Ow right, thanks BoredMormon, now it works.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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! :)
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    JoeStrout likes this.