Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug BUG: modifying the prefab with link to scriptable.

Discussion in 'Prefabs' started by Dmitriy_981, Mar 14, 2023.

  1. Dmitriy_981

    Dmitriy_981

    Joined:
    May 18, 2018
    Posts:
    10
    Hi there! I am using Unity 2021.3.15f1.

    What am i doing: I generate the scriptable through the code. After that I save the link of scriptable in new created prefab (is nested from another one). Lets call the prefab with link is LinkPrefab. My workflow:
    1) (Code example below) Check the folder, where must be scriptable. If not empty - remove all. Else - creating empty directory.
    2) Than I generate the scriptable and save them in new directory.
    3) (Code example below) After that I should create nested prefab. I create that, and check FileSystem to old prefab existing. If not exist - create new. If exist - replace link on scriptable using EditPrefabContentsScope.

    Problem reproduce:
    1) before generation, old prefab and old scriptable must be existed.
    2) I generate new scriptables, which replace old scriptables and change the link in LinkPrefab.
    3) If I entry in LinkPrefab - all correct. If I entry in another prefabs (lets call PrefabContainer), that contains LinkPrefab - all correct. Bit when I resave the PrefabContainer, exit from prefab edit mode and entry again in PrefabContainer - all links to scriptable in LinkPrefab will be null and marked modified. If i revert them, save and reenter in PrefabContainer - they all the same will be null. I opened the PrefabContainer through Notepad after reverting modifiers - the modifier didn't revert (see attached image).

    I didn't check that with non nested prefab, but I think, that problem will be reproduced again.
    Has anyone experienced this? Or maybe unity specialists can help me?

    Paragraph 1 code.
    Code (CSharp):
    1. StringBuilder builderScriptable = new StringBuilder("Assets/ScriptableObjects/Game/");
    2.         builderScriptable.Append(transform.name);
    3.         builderScriptable.Append("/{0}.asset");
    4.      
    5.         _folderName = builderScriptable.ToString();
    6.         string directory = Path.GetDirectoryName(_folderName);
    7.         if (Directory.Exists(directory))
    8.         {
    9.             string[] files = Directory.GetFiles(directory);
    10.             for (int i = 0; i < files.Length; i++)
    11.             {
    12.                 File.Delete(files[i]);
    13.             }
    14.  
    15.             files = null;
    16.         }
    17.         else
    18.         {
    19.             Directory.CreateDirectory(directory);
    20.         }
    Paragraph 3.
    Code (CSharp):
    1. TestPrefab newPrefab = (TestPrefab)PrefabUtility.InstantiatePrefab(_originalPrefab);
    2.         newPrefab.transform.position = Vector3.zero;
    3.         newPrefab.transform.rotation = Quaternion.identity;
    4.         newPrefab.name = transform.name;
    5.      
    6.         string prefabPath = $"Assets/Prefabs/Game/{transform.name}.prefab";
    7.         if (!File.Exists(prefabPath))
    8.         {
    9.             PrefabUtility.SaveAsPrefabAsset(newPrefab.gameObject, prefabPath);
    10.         }
    11.         else
    12.         {
    13.             using var editingScope = new PrefabUtility.EditPrefabContentsScope(prefabPath);
    14.             TestPrefab prefab = editingScope.prefabContentsRoot.GetComponent<TestPrefab>();
    15.             prefab.Info = info;
    16.             EditorUtility.SetDirty(prefab);
    17.             PrefabUtility.RecordPrefabInstancePropertyModifications(prefab);
    18.         }
    19.      
    20.         DestroyImmediate(newPrefab.gameObject);
    21.         DestroyImmediate(gameObject);
     

    Attached Files:

    Last edited: Mar 15, 2023
  2. Dmitriy_981

    Dmitriy_981

    Joined:
    May 18, 2018
    Posts:
    10
    Also after generation I has next error in console.

    Code (CSharp):
    1. NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
    2. UnityEditor.SerializedProperty.Verify (UnityEditor.SerializedProperty+VerifyFlags verifyFlags) (at /Users/bokken/build/output/unity/unity/Editor/Mono/SerializedProperty.bindings.cs:326)
    3. UnityEditor.SerializedProperty.NextVisible (System.Boolean enterChildren) (at /Users/bokken/build/output/unity/unity/Editor/Mono/SerializedProperty.bindings.cs:337)
    4. UnityEditor.GenericInspector.OnOptimizedInspectorGUI (UnityEngine.Rect contentRect) (at /Users/bokken/build/output/unity/unity/Editor/Mono/Inspector/GenericInspector.cs:120)
    5. UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Editor/Inspector/InspectorElement.cs:629)
    6. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/build/output/unity/unity/Modules/IMGUI/GUIUtility.cs:189)
     

    Attached Files:

  3. Dmitriy_981

    Dmitriy_981

    Joined:
    May 18, 2018
    Posts:
    10
    I think, I found the problem. And it's because in LinkPrefab saving wrong link. In Unity Inspector all visible correct, but when I open LinkPrefab through notepad - the link is empty. And the problem, to my mind, with the saving prefab through EditPrefabContentsScope or the creating asset and getting link to that. Code for creating asset is below.

    Can anoyone help me?

    Code (CSharp):
    1. string path = string.Format(_folderName, root.name);
    2. AssetDatabase.CreateAsset(infoAsset, path);
    3. info = infoAsset;