Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting referenced GUID from SerializedProperty

Discussion in 'Scripting' started by cnlohr, Jan 2, 2022.

  1. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    For whatever reason, I routinely have difficulty with some references getting unhooked when re-importing projects, or unity packages. I am unconcerned with that, because my workflow uses git and I don't save my entire project tree, just the files needed.

    Anyway, I want to programmatically re-associate the broken references. I can see, in the .scene and .prefab raw files (if you open in a text editor) they have a "programSource" field with a GUID which references the correct GUID in the meta file for the associated asset. My thought is I can just force-re-associate them.

    I want to get a copy of that GUID.

    Code (csharp):
    1.  
    2.             var serializedUdon = new SerializedObject(brokenUdon);
    3.             var property = serializedUdon.FindProperty("serializedProgramAsset");
    4.             // property now contains the property, but .objectReferenceValue is null.
    5.             // how can I get the GUID referenced here, and then look it up myself?
    6.  
    If I could get the GUID, I know I can use "AssetDatabase.GUIDToAssetPath" and "AssetDatabase.LoadAssetAtPath" to resolve the object.
     
  2. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    Still curious about this - but was able to work around my underlying issue by reimporting the underlying prefab assets. Not sure why Unity gets so confused when it comes to assets.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Are you properly source-controlling the .meta files? They are not optional. The name of a file or class is NOT used to maintain connections in Unity. Only the GUID contained in the .meta file. Source control them.