Confusion about prefabs!

Hello,

I’m currently about two thirds of the way through the Block Blaster section in Unity 2D. I’m having some issues with my prefab canvas! At the moment I have a start screen and a game over screen. Obviously I want a couple of buttons on each of these for starting and quitting the game. However, when I add buttons to the scene and next them under the Game Status > Game Canvas hierarchy, the buttons are applied to all scenes!

I’m not applying the buttons to either the Game Status or Game Canvas prefabs, they’re still greyed out which I thought meant they were unique changes to that instance of the Canvas, in that scene. I’ll post a couple of pictures to show what I mean :slight_smile:

The only solution I have found so far is to create a second Canvas in each scene, but I don’t know if this is a proper solution and/or good practice. Hope you can help!

Game Status Prefab
Hierarchy before In Game

Hi,

In your first screenshot, you are in the prefab mode. I can see that because the title says “Game Status”. In your second screenshot, it says “Start Menu”. That’s the scene mode. Since the Game Status game object is blue, it is connected to a prefab. If you make changed to the prefab, these changes get automatically assigned to the Game Status game object in the scene.

If you have a Game Status game object in each scene and if those game objects are connected with the “Game Status” prefab, the changes in the “Game Status” prefab get applied to all aforementioned “Game Status” game objects in the scenes.

Maybe it does not make sense to create a prefab in this case because you want to create different GUIs. A prefab is supposed to make your life easier. If you have to change a lot in the game object which is connected to the prefab, a new prefab for this game object or no game object might make sense.

To disconnect the game object from the prefab, click the right mouse button on the game object, then on “Unpack Prefab Completely”. Bear in mind that the game object cannot get reconnected.

Did this help?


See also:

1 Like

Is Game Status a singleton?

Making a different prefab is definitely a helpful idea. I followed the lessons exactly and discovered this issue, could it be worth suggesting the use of a separate prefab for the Start Game and Game Over scenes in the course?

Just to clarify, are the changes applied even though I’m not clicking ‘Apply All’ on the prefab objects?

It is, we implemented it so that the score carries over from level to level :slight_smile:

If you change a game object in the Hierarchy and click on “Apply all”, the connected prefab will get changes, and the prefab will update the game objects which are linked with it and its fields respectively.
If you change something directly in the prefab, all game objects that are linked to the respective fields of the prefab get updated. The difference between prefabs and game objects is crucial.

Is Game Status a singleton?

It is, we implemented it so that the score carries over from level to level :slight_smile:

Then as far as I know, this has nothing to do with prefab issues.

Game Status is a singleton so it will remain in play and carry over to the next levels. Your canvas and all it’s buttons are children of Game Status and will carry over with it.

That’s a useful summary :slight_smile:
So if I don’t click on Apply All after altering an object nested under a prefab in one scene, it won’t propagate the changes to the same prefab in other scenes?

That makes a lot of sense actually. Is the use of a second canvas a viable option here or is that bad practice? Is there another solution? :thinking:

I typically don’t have canvases that go from level to level, personally. I imagine there are lots of ways to doing these things, but yes, I often have multiple canvases in my scene and have a “Canvas Manager” that activates them and deactivates them as appropriate.

If you want, you can even put those extra buttons you made on a separate canvas that only appears on that screen and just have both canvases working at the same time, but only one carries over with the singleton when the next scene is loaded.

That’s incredibly useful! I really appreciate it :slight_smile: I will try out a few things and see what works for me :smiley: Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms