Search Unity

Issues when Instantiating UI prefabs [SOLVED]

Discussion in '2D' started by gonzo_gp, Sep 25, 2016.

  1. gonzo_gp

    gonzo_gp

    Joined:
    Nov 14, 2012
    Posts:
    2
    Hi everyone,

    I thought I will share this bit of knowledge with the community.

    In my app I was trying to instantiate UI Prefabs by code in runtime into the game and it was working in the Editor and in some Android devices (Android 4.4.2 and Intel CPU), but not all (Android 5 and 6 and Adreno GPU). I was doing all the suggestions found on the net:
    - use .SetParent(parentObj, false); instead of .parent = parentObj;
    - Instantiate under a Canvas Object
    - Have an Event System in the scene
    - Made 100% sure the code run and the object was in the scene and in the same position as in editor.
    - Check the game was not halted in any way waiting for a coroutine or line code to render next frame.
    - check possible alpha issues (as my code was fading in elements from the prefab)

    The prefab was not showing up in some devices.
    In the editor at runtime it worked fine, but if I dragged the element manually (not at runtime) it didn't show up either, unless I changed the Canvas render mode to one mode and come back to the previous one. So this bugged me.

    And I realise the prefab had a missing component in the hierarchy. The hierarchy was like this:
    UIHolder
    - button1
    -- buttontext1
    - button2
    -- buttontext2

    Button1 and button2 were buttons, and buttontext1 and 2 were Text. All of this had a Canvas Renderer
    UIHolder only contained a RectTransform and a custom script. It didn't have a Canvas Renderer.

    As soon as I added a Canvas Renderer to UIHolder, the prefab showed in all devices. :) So I assume that every element should have a Canvas Renderer, specially if it has children.

    So I thought I will publish this for everyone to know.

    However I have a question to Unity fellas, I am not sure if this is a expected behaviour or not. But it was very misleading that it worked in certain devices and not in others. It might be related on how the GPU works? The android device I tested with is slow old device... Perhaps that helped to show the elements?
     
    Last edited: Sep 25, 2016