Search Unity

will the Guid of a Prefab always stay the same?

Discussion in 'Scripting' started by craig4android, Jun 11, 2019.

  1. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    Code (CSharp):
    1. var res = AssetDatabase.FindAssets("t:GameObject", new string[] { "Assets" });
    2.         foreach (var Guid in res)
    3.         {
    4.      //will the Guid always be the same? Even if I close the editor or drag the GameObject to an other path?
    5.         }
    Sure I could test, it but it seems to be the same after restarting, but I don't want to crash my whole project because I find out at some point that the Guid can randomly change.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
  4. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    763
    If you're using source control (which you really should be), a GUID change will show up as a change in GIT. So you'll know if a GUID has changed.
     
    Joe-Censored and Kurt-Dekker like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,737
    What @Carwashh said... put your project under git right now, it's easy. Think of it as a save game cartridge for developers.

    The easiest way to inadvertently change a GUID is if you duplicate an object (or bunch of objects) outside of Unity.

    When Unity regains focus it will see the dupes and reassign half of them, and if it's the ones you were already using, you'll have broken links.

    If you dupe or move stuff, dupe or move it from within the Unity project window.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I believe the GUID is how Unity tracks stuff for itself. For instance, when you drag a reference into a public field in the inspector, you're actually assigning a GUID. As long as that reference is still valid, the GUID is still valid.

    If you configure Unity to save *.meta files for your assets (which you normally have to do if you're using version control), then the GUID is stored in that *.meta file. As long as you keep that meta file with the asset, the GUID will be there. If you delete the meta file, or move the asset without moving the meta file, that's when references will break.
     
    Dennis_eA and Kurt-Dekker like this.