0
\$\begingroup\$

I'm building a game at the moment and a classic issue has come up, of having a complicated prefab variant in use, which happens to have a fundamental problem in its design and needs to be replaced. This is often done to a new prefab variant that only barely differs, and I'll typically name it something like <original name>.2 to differentiate.

(Usually it's something silly like forgetting to rotate by an empty parent game object that doesn't have an animator attached, something like that... nothing that can easily be swapped out of the original prefab.)

This works great. However, there's always the possibility that I'm going to be popping that deprecated original prefab into a slot that expects the new one, and warnings like we get with the [Obsolete] annotation on class code would be really useful and quite the time saver, I wouldn't even have to run the program to know I've done something wrong. Since prefab variants follow much the same pattern as classes and subclasses, I'm wondering:

Do prefabs have an equivalent to the [Obsolete] attribute? Can I tell Unity directly to keep it, but not let me use it without throwing a fuss?

\$\endgroup\$
1
  • \$\begingroup\$ Have you tried adding an extra object to the variant as a child, with something like a huge obnoxious pink sphere that's visible from orbit, or a script that prints warnings to the console on editor load? \$\endgroup\$
    – DMGregory
    Commented Sep 1, 2022 at 0:49

1 Answer 1

3
\$\begingroup\$

Personally I would not recommend leaving deprecated prefabs in your project. If you're not careful, those deprecated files are extremely confusing for new team members, or when doing maintenance years down the road when you no longer remember every little detail. It also makes searching your project more tedious since your search results will include deprecated files.

I'd suggest either deleting the bad prefab after creating a new version, or just modifying the bad prefab to fix it. If you ever need the old version again (why would you?) - that's what version control is for.

If you absolutely must keep the old prefab for some really legitimate reason and not out of a fear of ever deleting anything, you should rename it and include the word "Deprecated" in the new name.

Rationale

Deprecation is normally used in the context of public APIs/packages, where your team is creating code or other assets that will be used by other teams. You mark an API or other asset as deprecated when it's no longer supported and will be removed in a future version; the only reason you're keeping it around is for backwards compatibility, so developers that use your API/package don't have to immediately update their code to keep their project working.

When you are working on an internal project solo or with a small team, there's not a lot of reason to keep around deprecated files. You don't have to worry about breaking someone else's project when you remove an old version of a file (unless you're working on a shared library that is used by multiple internal projects). The biggest concern is if you are working on a large team where multiple developers might be currently working with the asset that you want to get rid of; then you may need to coordinate with them to make sure you don't do something that causes merge conflicts.

If you want to replace a broken prefab because it's not possible to fix the prefab, there's a way to replace the prefab, (usually) without breaking existing references to that prefab:

  1. Create the new prefab that works correctly.
  2. Minimize Unity and do not open it again until you reach step 5.
  3. In your operating system's file browser (e.g. Explorer, Finder), find and delete the prefab file, but not the corresponding .meta file.
  4. Delete the .meta file for the new prefab. Rename the new prefab with the same name as the old prefab.
  5. Open Unity again.

What we've done here is replace the old prefab with a new prefab with the same GUID (this is defined in the .meta file). Any components that referenced the old prefab by GUID will generally still have their references to the new prefab. However, references to a specific script on the prefab will no longer be valid if the new prefab doesn't have the same script.

\$\endgroup\$
4
  • \$\begingroup\$ I must disagree with you on your rationale for deprecation; there are many occasions in which I will be writing code which I myself will use in multiple projects or re-use (or reference) down the line. It's not exclusively for other teams, it's also for me, maybe a year later, when I've long forgotten what I was thinking when I built it. It's an annotation, after all; the compiler couldn't care less. It's a "this will do it, but really shouldn't" tag. I may start simply naming them with "deprecated"—while I also appreciate your file manager workaround, the direct answer seems to be "no". \$\endgroup\$ Commented Sep 1, 2022 at 9:53
  • \$\begingroup\$ Ah, the other reason I'm hesitant to immediately delete a prefab is that, regardless of what was wrong with it, portions of it clearly worked and are worth porting to the new prefab with adjustments. This is a terrible time to delete it, I think, as it would waste time and work resources and I'd be repeating myself. Thus, I leave them lingering in the file system until I know I've got something better implemented. \$\endgroup\$ Commented Sep 1, 2022 at 9:55
  • \$\begingroup\$ @MichaelMacha Regarding code, once you've replaced the deprecated code with new code, why keep the deprecated code around? Why would you need to look at broken code a year later when you've forgotten what you were thinking when you built it? If you absolutely need to review it later, that's what version control (e.g. Git) is for. If you aren't using a version control system, that's a big problem you need to fix. \$\endgroup\$
    – Kevin
    Commented Sep 2, 2022 at 21:13
  • \$\begingroup\$ @MichaelMacha Of course you don't delete the old prefab until you're finished assembling the new prefab. However, I'd suggest deleting the old prefab as soon as the new prefab is ready, not keeping it around with some kind of indicator that it's deprecated. \$\endgroup\$
    – Kevin
    Commented Sep 2, 2022 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .