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 Only Activates When Clicked Twice?! [HELP!]

Discussion in 'Scripting' started by ExbowFTW, Dec 16, 2015.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    I have a button under a canvas that when clicked should setactive 5 gameobjects... However, when I run the game, I have to click the button twice for the gameobjects to finally appear... I checked as you can see with Debug.Log("CLICK CLICK"), and when I click it the first time it does detect the click, but when it Setactive's the 5 gameobjects, they don't actually appear until you click the button again. Here is the script:

    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingUnityEngine.UI;
    4. usingSystem.Collections;
    5.  
    6. publicclassTitleButtons : MonoBehaviour {
    7. GameObjectLoadingCanvas;
    8. GameObjectMainCanvas;
    9. GameObjectsphere;
    10. GameObjectMoreGamesWindow;
    11. GameObjectEightXEightButton;
    12. GameObjectEightXEightText;
    13. GameObjectBackButtonGames;
    14. GameObjectComingSoonText;
    15. GameObjectWebsiteURLText;
    16.  
    17.  
    18.  
    19. voidAwake () {
    20. MoreGamesWindow = GameObject.Find ("MoreGamesWindow");
    21. EightXEightButton = GameObject.Find("8x8Button");
    22. EightXEightText = GameObject.Find("8x8Text");
    23. BackButtonGames = GameObject.Find("BackButtonGames");
    24. ComingSoonText = GameObject.Find("ComingSoonText");
    25. WebsiteURLText = GameObject.Find("WebsiteURLText");
    26.  
    27. LoadingCanvas = GameObject.Find ("LoadingCanvas");
    28. MainCanvas = GameObject.Find ("Canvas");
    29. sphere = GameObject.Find ("Sphere");
    30. }
    31.  
    32. //Usethisforinitialization
    33. voidStart () {
    34. MoreGamesWindow.SetActive (false);
    35. EightXEightButton.SetActive(false);
    36. EightXEightText.SetActive(false);
    37. BackButtonGames.SetActive(false);
    38. ComingSoonText.SetActive(false);
    39. WebsiteURLText.SetActive(false);
    40.  
    41. LoadingCanvas.SetActive (false);
    42. }
    43.  
    44. //Updateiscalledonceperframe
    45. voidUpdate () {
    46.  
    47. }
    48.  
    49. publicvoidClicked () {
    50. if (gameObject.name == "PlayButton") {
    51. LoadingCanvas.SetActive(true);
    52. MainCanvas.SetActive(false);
    53. sphere.SetActive(false);
    54. Application.LoadLevel("Game");
    55. }
    56. if (gameObject.name == "MoreGamesButton") {
    57. MoreGamesWindow.SetActive(true);
    58. EightXEightButton.SetActive(true);
    59. EightXEightText.SetActive(true);
    60. BackButtonGames.SetActive(true);
    61. ComingSoonText.SetActive(true);
    62. WebsiteURLText.SetActive(true);
    63. Debug.Log("CLICK CLICK");
    64. }
    65. if (gameObject.name == "ShareButton") {
    66. Application.CaptureScreenshot("Screenshot_Swervy.png");
    67. }
    68. if (gameObject.name == "TwitterButton") {
    69. Application.OpenURL("https://twitter.com/PurifiGames");
    70. }
    71. if (gameObject.name == "FacebookButton") {
    72. Application.OpenURL("https://www.facebook.com/Purifi-950120728411407/timeline/");
    73. }
    74. if (gameObject.name == "InstagramButton") {
    75. Application.OpenURL("https://instagram.com/purifigames/?hl=en");
    76. }
    77. if (gameObject.name == "8x8Button") {
    78. Application.OpenURL("https://itunes.apple.com/us/app/8x8/id1049171469?ls=1&mt=8");
    79. }
    80. }
    81. }
    82.  
     
  2. SoftwareEngineer

    SoftwareEngineer

    Joined:
    Jan 4, 2014
    Posts:
    52
    Your code is too long. I advice you to put only necessary block of code.
     
  3. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    I don't think that the problem is here, you have to look if there are is someone else who is making your object inactive. I suggest to Debug.Log the activeness of one of the objects and see if there is something strange.
     
    current666 and oanamoraru like this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or do you have a UI component "over the top" which is blocking the button and reacting to the first click itself?
     
  5. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Well, my "MoreGamesButton"'s parent is a Canvas. There are other buttons that are also "siblings" of "MoreGamesButton", and they all work... More Games Button is the only one that doesn't for some very strange reason.
     
  6. Sun-Pengfei

    Sun-Pengfei

    Joined:
    Nov 12, 2015
    Posts:
    35
    I just found if an UI object is set to inactive before running the game in unity editor, then you need to setActive twice to actually show it. Not sure why.
     
    Deleted User likes this.
  7. dijutsu

    dijutsu

    Joined:
    Sep 11, 2014
    Posts:
    1
    Make sure that any scripts attached to the UI object doesn't have a gameObject.SetActive(false) in its Start function. I ran into this issue before, and was beating my head on why I had to call it twice, then remembered that everytime an object changes states from inactive to active, the start function is called!
     
  8. Sun-Pengfei

    Sun-Pengfei

    Joined:
    Nov 12, 2015
    Posts:
    35
    Thanks for your info, but mine is different. If the objects are set as active when click the Play button, all works fine including using code repeatedly changing the active state(only call once works)
     
  9. WilB

    WilB

    Joined:
    Oct 25, 2012
    Posts:
    17
    Thank you! I didn't know - but I should have known - that the Start function is called whenever an object is made active. This was driving me mad.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This is an old thread, but that is not true! :) Start is only called once for the lifetime of the script. It's called the first time it's enabled, and that's it. If you disable and re-enable the script (or game object), it's never called again.
     
    SparrowGS likes this.
  11. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    368
    True. Perhaps he meant "OnEable()" ;)
     
    SparrowGS likes this.
  12. Julien_at_work

    Julien_at_work

    Joined:
    Aug 9, 2018
    Posts:
    35
    I ran into the issue myself that Sun-Pengfei reported, is this a known bug?
    (UI objects disabled in the editor need to have SetActive called twice (even just in direct succession) to apply the passed state the first time)
     
  13. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Please do not reanimate old threads. Open a new thread for your questions. Most likely it's not a Unity bug. Please give us more information, code, how do you do what you do (in CODE tags!) and screenshots about your inspector (the subject game object selected of course).
     
  14. Julien_at_work

    Julien_at_work

    Joined:
    Aug 9, 2018
    Posts:
    35
    Here's a repro project. After Play, click a button, a subobject will be visible (expected), but no label above (unexpected), click it again, see the label appear above.
    Now restart playmode, tick the toggle at the top (checked=true) and click a button once, see the label appear above.
    Two places disable the label in question at awake and even though that shouldn't matter it seems as though the deactivations did stack.
     

    Attached Files:

  15. nabeelasad2007

    nabeelasad2007

    Joined:
    Jul 23, 2021
    Posts:
    6
  16. Spikee_wave

    Spikee_wave

    Joined:
    Feb 27, 2018
    Posts:
    5
    Button is not invoking OnClick event on first click when previously you clicked something in editor window (inspector for example)