UI Prefab children not visible in prefab edit mode?

[Edit: Ok I found the root cause of this problem, I think. When editing a Canvas prefab asset that has a Canvas Scaler component directly from the project folder, the Canvas Scaler is disabled with the warning “non root canvases cannot be scaled”. So the scale of the root transform of the prefab is 0,0,0.

This does not happen when editing an instance of the Canvas prefab in a scene. The Canvas Scaler component is processed fine.

I guess this issue is documented somewhere?

Thanks all who responded here and guided me in any direction!
]

Here is a puzzle that I hope someone can help me with, it must be something really dumb and I must be missing it:

I have prefab that is composed of a Canvas screen space overlay, Canvas a Group, and a bunch of UI and Text Mesh Pro child elements

When I open the prefab to edit it directly from the project folder window, all of the children are invisible.

When I open it from the scene hierarchy where I authored it, everything is fine - I can see it all.

the Canvas Group alpha of the prefab instance in the scene is set = 0;

What is going on?

It’s like somehow the prefab’s CanvasGroup alpha value from the scene is remembered by the prefab, even though in the project view it appears to be set to alpha = 1?

Or what?

Thank you in advance if you can shine any light on this vexing problem!

Hi. Gordon.
I think Canvas group alpha does not affect the prefab’s alpha.
I can not understand what your goal. Please contact me if you solve this problem.
Telegram-@Royalord1025

Sounds like perhaps you dragged in the prefab and your code set its alpha to zero. This is an unfortunately common workflow error in Unity. Here’s more reading:

Instancing and prefabs in Unity and naming your variables:

If you name your public / serialized fields (variables) as simply “thing” then you set yourself up for confusion.

If something is a prefab, name it that, such as public GameObject ThingPrefab; and only drag prefabs into it. Prefabs are “dead things (assets on disk) waiting to be Instantiated.”

If something is already in the scene or you Instantiate<T>(); it in code, then store the result in a properly-named variable such as private GameObject ThingInstance;

Naming is hard… name things WELL to reduce your own confusion and wasted time.

1 Like

Hey thanks so much for your reply. I don’t quite understand what I think you are saying, or perhaps I need clarify the situation:

The prefab implements a “UI window” feature. Originally I authored the window directly into a scene, and when I got it all set up I created a prefab using the window’s parent canvas / canvas group. This is convenient for editing without opening the scene (if & when you can see everything its convenient!)

When I edit the scene, usually the prefab’s Canvas Group alpha = 0. I may set it to 1 if I want to edit the prefab in situ (i.e. open the prefab within the scene).

Whether or not I save the Canvas Group alpha override at that point doesn’t matter to the execution of my game, because at start up my code sets the alpha = 0 anyway, until such time as the window needs to be shown. But that doesn’t matter because with this problem we are talking purely about editor behavior.

Now, in the editor, without the game running, I look at the prefab in project folder and it’s inspector shows me its Canvas Group alpha == 1. But if I try to then edit the prefab directly from the project folder window, its children are all invisible, even though the alpha still says 1 in the prefab edit mode (or what ever it is called).

Is that better or worse than my original description? I hope that makes sense because I really appreciate any further clarification you care to offer!

The game runs fine, it’s not a problem with executable code (that I can think of). I’d just like to be able to edit the prefab without having to first open the scene that it is in, and launching prefab edit mode from the instance that is in the scene.

I know I have been able edit other similar prefabs directly without opening the scene they are in and it hasn’t been a problem. However, I have come across this problem more than once as well.

Could it be because the prefab root object does not have a canvas component? Not sure if UI components will show if there’s no canvas to render them.

1 Like

Could it be that because the Prefab is a canvas does not have a camera in it, that the Screen Space Overlay Canvas and its children are not drawn?

I keep thinking there is some button on the toolbar in prefab edit mode will rectify the situation…

Ignore this I just saw that you already specified that it has a canvas component. Another thing I can think of is make sure the root transform values are correct. Changes you make to the transform on a prefab instance that is inside a scene do not automatically migrate to the prefab’s transform, so make sure the values in the prefab are not set in a way that stops the children objects from showing.

Oooh, I’m sorry to miss that… is it always in editor except when running? I ask that basically there definitely documented instances of certain things in the UI system that can get out of whack and not be valid when you’re paused.

Part of the reason is this delicate process, but there are also outstanding bugs at editor time:

Serialized / public fields in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

  • what the class constructor makes (either default(T) or else field initializers, eg “what’s in your code”)

  • what may be saved with the prefab

  • what may be saved with the prefab override(s)/variant(s)

  • what may be saved in the scene and not applied to the prefab

  • what may be changed in the scene and not yet saved to disk

  • what may be changed in OnEnable(), Awake(), Start(), or even later

Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

Here’s the official discussion: https://blog.unity.com/technology/serialization-in-unity

If you must initialize fields, then do so in the void Reset() method, which ONLY runs in the UnityEditor.

Here’s more nitty-gritty on serialization:

Field initializers versus using Reset() function and Unity serialization:

To avoid complexity in your prefabs / scenes, I recommend NEVER using the FormerlySerializedAsAttribute

1 Like

Make a screenshot of your prefab transform. Maybe it’s scale, maybe anchors, it could be anything that explains why you see it in hierarchy - because it has right properties there

1 Like

OK, I think I found it - I’m blind - I will edit my original post above.

I guess canvas prefabs can’t calculate their scale when you edit from their project folder?.

The prefab has a Canvas Scaler component that looks good in the prefab inspector in the scene, and also if I open the prefab from the scene instance to edit. Everything hunky dory.

But when I edit the prefab by double clicking on it in the in the project folder the Canvas Scaler shows “non root canvases cannot be scaled”

I figured it out because I noticed the scale of the prefab root was 0 when editing the prefab asset the project folder. I can set the scale of the root canvas’s transform = 1,1,1 where it should be, but then all of the children suddenly a lot smaller than they should be, like the canvas needs that Scaler component even though the prefab does not allow it.

Anyone experience that?

Kurt, thanks so much for the detailed links - this is great information about prefabs I should have learned years ago.

1 Like

In Project Settings / Editor you’ll find this:

So, make a scene that resembles your canvas, save it and drag it there. Now, all your UI prefabs will be opened in that template