Search Unity

Prefabs randomly change on their own

Discussion in 'Editor & General Support' started by Deibu, Aug 19, 2019.

  1. Deibu

    Deibu

    Joined:
    Feb 24, 2013
    Posts:
    34
    Hi,

    We're having issues very similar to these two posts:
    https://forum.unity.com/threads/object-references-become-missing-after-building-the-game.561574/
    https://forum.unity.com/threads/uni...ds-randomly-change-for-entire-project.568024/

    At random times (usually after entering play mode/recompiling/building), certain objects will lose references to prefabs (due to fileID or guid changing). Sometimes, a simple git discard or revert on a prefab will fix the issue... but other times, the prefabs have changed so much that it stays as a missing reference.

    Also, we've recently started noticing that on a few of our prefabs, certain scripts on them with enum variables have started to change randomly. We were using SteamVR's SteamVR_Behaviour_Pose script, and the everything was running fine for several minutes, but then the PoseAction value changed and caused all of our input to break. Upon exiting play mode, the prefab was also set to the new value.

    We've noticed this behavior since 2018, and we have been updating to the latest stable releases of Unity and are currently on 2019.2.0f1. This has happened to prefabs that have been migrated over to the new version of Unity, as well as all new prefabs in the latest version.

    There were some other examples that happened too with nested prefabs resetting to their base prefab, but it seems impossible to reproduce any of these behaviors.

    Thanks!
     
  2. wholesalebirbs

    wholesalebirbs

    Joined:
    Jul 13, 2017
    Posts:
    1
    This started happening on my project recently as well in Unity 2019
     
  3. luisfinke

    luisfinke

    Joined:
    Dec 2, 2017
    Posts:
    8
    Sorry to necro but just happened to me as well. Completely random and no changes shown in git
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    A frequent cause of changing prefabs is that your code is manipulating the prefab instead of the instance.

    A great defense against this is to always have separate variables, AND if you don't need the prefab instantiated multiple times, null it out to reveal lawbreakers.

    Code (csharp):
    1. [Header( "Drag in the prefab here...")]
    2. public GameObject PlayerPrefab;
    3.  
    4. // this is the actual player
    5. GameObject PlayerInstance;
    In your Start():

    Code (csharp):
    1. PlayerInstance = Instantiate<GameObject>( PlayerPrefab, position, rotation);
    2.  
    3. // let's keep everybody honest by invalidating the prefab reference, thereby
    4. // preventing them inadvertently changing the prefab on disk.
    5. PlayerPrefab = null;
     
  5. luisfinke

    luisfinke

    Joined:
    Dec 2, 2017
    Posts:
    8
    I'm certain it wasn't due to manipulating the prefab over the instance. In fact, it happened on a prefab that was referencing another prefab variant in its body, which suddenly showed "Missing Prefab" even though the prefab still existed and hadn't changed. I just had this happen to me again today. Very strange.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Are you moving things in your filesystem, eg, outside of Unity? You must ONLY move Unity things from within Unity.

    You really should be using proper source control so you can see unintended changes and revert them.

    Please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally setting Unity up (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place. Digital storage is so unbelievably cheap today that you can buy gigabytes of flash drive storage for about the price of a cup of coffee. It's simply ridiculous not to back up.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards
     
  7. luisfinke

    luisfinke

    Joined:
    Dec 2, 2017
    Posts:
    8
    None of those things were the case. I simply switched branches on the project and suddenly it happened. The issue was fixed by deleting the "Library" folder and regenerating it, but outside of the library folder, almost everything else would have been on source control and no changes were shown in git. So it likely had something to do with an auto-generated library file
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    BINGO. If you switch branches it is always best to close Unity first.

    At a minimum you should NOT allow Unity to gain focus until all the branch switch is fully done, otherwise upon focus Unity will "observe" an incomplete state of your working tree.

    More recent versions of Unity are even more problematic, as they start importing in the background.

    To get around the inconsistency, either close Unity for source control changes, or do a re-Refresh when you refocus Unity after a branch switch.