Search Unity

Button onClick() not being called

Discussion in 'Scripting' started by kittik, Nov 30, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    A button I am pressing does not call the function that should be attached to it.

    I have read the documentation and cannot see what I am doing wrong, any help would be appreciated. There are no errors within my code.

    I hope the below images prove helpful in assisting with my task.





     
    randallrmz likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    To start, use Debug.Log instead of print. There are situations where print() simply doesn't show up in the console, and I can't say for sure whether UnityEvent handlers are one of them. However, since LoadLevel would either be succeeding or throwing up a runtime error, you're probably correct that that section of code is not being executed.

    Have you tried linking to other functions in this button? Possibly builtin ones even - for example, you can set it to SetActive(false) on a GameObject. If it doesn't do anything you put in there, then it's an issue with the setup of the button itself - maybe it got duplicated and there's an identical button on top that's actually being clicked, or you've been modifying the events for the wrong button, for example. If it affects other things but not that script....something weirder is going on.
     
    randallrmz and kittik like this.
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thank you @StarManta, I am using Debug.Log now. The function is never called, I have removed all buttons, added a new one, given it the action within On Click (). The function never gets called.

    I believe it could be down to a script from an asset that I did not make. This asset (UFPS), disables the cursor at the start of play and locks it in a position.

    When I want to restart the game, one of my scripts adds a cursor by using -
    Code (CSharp):
    1. gamer.GetComponent<vp_FPInput>().enabled = false;
    2. vp_Utility.LockCursor = false;
    The first line removes the script which disables the cursor. The second line allows the player to then use a cursor. I have read at the foot of this page, that a locked cursor prevented being able to click a button.

    Perhaps a solution is to enable the cursor within vp_FPInput, not just disabling the function - I have tried finding how to enable the cursor and have it being usable for a few hours, to no avail.

    vp_FPInput allows the user to add zones where a mouse is usable. This suggests that when the script is enabled, the screen is not usable by default. By disabling the script, it should surely enable the screen - I do not see how this could be causing my button not to react.

    Might I be overcomplicating things?

    Could I possibly be somehow creating a 'ghost' cursor which is purely aesthetic? (Hence why it does nothing)

    Further help from others is welcome.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    do you have an eventsystem in your hierarchy?

    no eventsystem, no unityevents being called...
     
  5. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thank you @LeftyRighty, can't believe I missed that. I thought I had an EventSystem within my hierarchy. Clearly I was overcomplicating things.
     
    randallrmz likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    eventsystem should be added automatically when you add a UI "thing" and one doesn't already exist but it sometimes doesn't, haven't really found a reliable "why" to that, might be if there is an eventsystem in another scene or something the automatic creation skips out?
     
    kittik likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Its automatically created if you use the create menu to make a UI thing.

    Its not automatic if you get the UI thing any other way. Including if you manually add components to build a UI thing. Or if you bring a UI thing with DontDestroyOnLoad. Or if you copy paste a UI thing betwen sceens. Or if you create a UI thing from a prefab. Or if you create a UI thing via editor script. Or if you draw a UI thing on the screen with markers.

    You get the idea. Automatic is only when using the create menu.
     
    kittik likes this.
  8. Himanshu6

    Himanshu6

    Joined:
    Jul 8, 2020
    Posts:
    2
    Perfect solution. Thanks!