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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Button not working as Prefab

Discussion in 'UGUI & TextMesh Pro' started by KKondarev, Jul 3, 2015.

  1. KKondarev

    KKondarev

    Joined:
    Dec 16, 2013
    Posts:
    7
    Hi,

    I have the following issue: I have created a UI 'element' consisting of Panel, containing a Button and 3 Images. The Button itself has a Text, as it is by default. Everything is working great.

    However, when I create an empty prefab and drag the Panel there - basically to create a reusable asset - the structure is 'flattened', meaning the Button's label is at the same level as the Button itself. It is still visible, however I noticed, that when the prefab is instantiated, I can only click the button outside of the area of the label... which makes no sense, since the label is naturally displayed in the center of the button and takes most if the place.

    Any idea how to overcome this?
     
  2. KKondarev

    KKondarev

    Joined:
    Dec 16, 2013
    Posts:
    7
    No one have any idea?
    Or no one faced this issue so far?
    Seems to happen every time when a button is stored as prefab... so easy reproducible...
     
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,996
    pictures?
     
  4. Selene06

    Selene06

    Joined:
    May 3, 2015
    Posts:
    3
    Hi,
    Does anyone have any idea how this can be fixed?
    I seem to have the same problem. Whatever UI element with multiple objects in it is dragged in the project to create a prefab it will be flattened.
     
  5. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,996
    I know there were a few bugs with prefab and UI but I'm pretty sure it's fixed in 5.2
     
  6. Selene06

    Selene06

    Joined:
    May 3, 2015
    Posts:
    3
    Hello,
    Thanks for the reply. I have updated unity to 5.2, however I am still having issues.
    The problem is with UI elements which have multiple other elements in it, for example: a panel and within it 2 other buttons so this would look like:
    Panel
    - Button
    -Text
    - Button
    - Text
    If you drag the Panel into the project to make it a prefab, it will only remain:
    Panel
    - Button
    - Button
    The text part will be missing.This will occur with any element if there are more of them.

    Is there any way of fixing this?
    Thank you in advance for any feedback.
     
  7. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    21
    I'm having the same issue - not sure what is going on. Would love to know a solution, I'm sure its something I'm overlooking! Sleep will probably fix this :)
     
  8. Selene06

    Selene06

    Joined:
    May 3, 2015
    Posts:
    3
    Does anyone have a solution for this? :)
    Thanks.
     
  9. temeezer

    temeezer

    Joined:
    Jun 16, 2015
    Posts:
    6
    I have a following hierarchy:

    Panel
    - Button
    - - Text
    - Button
    - - Text
    ... etc.

    and the only problem that I have is that I can't open the Buttons inside the Panel prefab, but the Text elements are there when I drop the prefab into the scene. I just drag the panel element from the hierarchy into the prefabs folder and voilà. Using version 5.2.1p1.
     
  10. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    I tested things a bit and...
    Can't seems to be able to reproduce the bug. Even tried to prefab an incomplete UI by moving the panel out of the canvas and even then it still register the prefab correctly. Could you try to do it in a new project with nothing except the UI you try to work with? If it still doesn't work, you could send this new "semi-empty" project via the "Report Bug" from the Help tab inside Unity. If the problem is really not a closed case (meaning a bad bug that only happens to you), they could work on a fix.
     
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,985
    When you create prefab, in the project it only shows first level children, this is normal and the expected behavior. When instaniated or placed in the scene/hierarchy you will see the full structure.
     
  12. andrei_filip

    andrei_filip

    Joined:
    Mar 30, 2015
    Posts:
    7
    After Instantiating the the profab , make sure to set the game objects parent to a canvas. Not doing this will create an extra canvas with default properties, witch may not be what you want.
    Also if the action of the button is on a script that is not on the prefab, the event will be lost.
    Use button.onClick.AddListener to add the event when instantiating.

    hope it helps
     
  13. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
  14. Khazam

    Khazam

    Joined:
    May 6, 2016
    Posts:
    1
    I think you have to copy the EventSystem GameObject with you(with the prefab), because I tired to delete an EventSystem and suddenly the UI start not responding. When I got it back it start working again.
     
  15. Malice983

    Malice983

    Joined:
    May 21, 2016
    Posts:
    13
    I tried this. I dragged the button into the Assets panel. I have a prefab folder but that makes no difference for the answer I give. Then the original button is blue, showing it is a prefab. When I put the button in another scene it works as intended.
     
    Minxer likes this.
  16. Happyday

    Happyday

    Joined:
    Oct 10, 2015
    Posts:
    3
    you must add Graphic Raycaster component on the parent transform and then the button can use again.
    upload_2016-12-13_11-12-49.png
     
  17. Gevorg6

    Gevorg6

    Joined:
    Jan 1, 2017
    Posts:
    1
    EventSystem might be missing...
     
  18. cloudymax

    cloudymax

    Joined:
    Jul 18, 2015
    Posts:
    3
    This resolved the issue for me, there was no event system in my prefab
     
  19. CrusaderGaming

    CrusaderGaming

    Joined:
    Sep 2, 2018
    Posts:
    1
    I was having the same issue. This fixed my problem. I am using Unity 2019.2.5f1
     
    alexandre_bergeron_gd likes this.
  20. arduman4

    arduman4

    Joined:
    Dec 22, 2019
    Posts:
    1
    Genius... I never knew what is it for, so I didn't suspect it. Thank you.
     
    DRapp likes this.
  21. ghnsharma_unity

    ghnsharma_unity

    Joined:
    Mar 29, 2020
    Posts:
    1
    I was facing the similar issue, when we import the UI prefab in new Scene button does not work as there is no EventSystem there. Add new EventSystem and button starts working.
     
    alexandre_bergeron_gd and DRapp like this.
  22. Abhishek1938

    Abhishek1938

    Joined:
    Nov 24, 2018
    Posts:
    2
    I got the solution of this problem. I was getting the same problem but somehow I solved it. so if you are making the prefab of UI then you shuld know that UI works with event system that comes with the UI automatically in hierarchy. so if you are making the prefab of only UI, so now you can add a new canvas and after appearing of event system just remove the new canvas and thne your UI will work fine.