Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Prefabs invisible in build but not in scene- or game views

Discussion in 'Prefabs' started by nersons_skatt, Mar 16, 2021.

  1. nersons_skatt

    nersons_skatt

    Joined:
    Dec 11, 2020
    Posts:
    9
    Hi all,

    I have a scene with a 3D camera but a Screen Space - Overlay canvas. The canvas shows different panels where cards go (it is a card game). Each card is a UI panel with graphics and text etc, and always placed inside the main canvas. A card is a prefab, which is instantiated at runtime depending on which cards you add to your deck. I also have some instances of this prefab already present in the scene from load.

    Here's my problem: The cards already in the scene when it loads can be seen. But all the cards that are instantiated during run-time are invisible. I have made Debug.Logs all over the place. I don't get any errors. The game objects are indeed instantiated, and they are moving correctly on the screen according to the coordinates I can see through logging. In both the scene view and the game view in the Unity Editor, they can be seen. However, when I build and run the game, they are invisible. Still there (Logs look the same), but invisible. As I said, the exact same prefab can be seen if it is already part of the hierarchy from the start, but not if instantiated during runtime.

    What could this possibly be?? It is extremely frustrating that a build doesn't work as it does in the editor.

    (Just some more background info, just in case it is relevant:
    I started as a 2D project, but after a while I just switched over to a 3D camera since I needed that.
    Some threads suggest I may have problems with camera Z-coordinate in relation to these invisible panel z-coordinates. However, this doesn't seem likely, as the camera is a 3D camera. I can also zoom out (move the camera backwards) without effect.
    Others have suggested something may be wrong with the shader. But since the already instantiated prefab looks just fine, this shouldn't be a problem, right?
    Some say that these problems are due to scripts not executing in the same order as in the editor. However, since I can see in the logs that the card panels are created and moving, and get no errors, something else is the problem here, it seems...)

    Image for show:
     
  2. nersons_skatt

    nersons_skatt

    Joined:
    Dec 11, 2020
    Posts:
    9
    OK, so after a lot of time, I found out that the cards were not, in fact, children of this canvas. So they had the correct coordinates, but within the wrong canvas, or perhaps even in world space instead of screen space.

    The difference between the build and the editor is probably the execution order of scripts, among which was the script that assigned the correct canvas to a variable.