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. Dismiss Notice

Prefab is "forgetting" public variables on instantiation

Discussion in 'Editor & General Support' started by Good_Punk, Aug 23, 2021.

  1. Good_Punk

    Good_Punk

    Joined:
    Aug 6, 2014
    Posts:
    81
    Hello, I have a problem where I can't find a solution:

    I'm adding objects to a list on a script on a prefab with this code:
    Code (CSharp):
    1.         if (UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null) // In Prefab Mode
    2.         {
    3.             RoomConfig currentConfig = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage().FindComponentOfType<RoomConfig>();
    4.             string path = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(Selection.activeObject as
    5.                  GameObject).assetPath; // Get the prefab path in disk
    6.             GameObject prefabRoot = PrefabUtility.LoadPrefabContents(path); // Load the contents of the prefab.
    7.             SerializedObject saveConfig = new SerializedObject(currentConfig);
    8.             currentConfig.AddPositionMarker(this); // Add a reference to this object to the RoomConfig
    9.  
    10.             saveConfig.Update();
    11.             saveConfig.ApplyModifiedProperties();// Apply after editing so that the values are stored
    12.  
    13.             try
    14.             {
    15.                 PrefabUtility.SaveAsPrefabAsset(prefabRoot, path); // Save the prefab to disk    
    16.                 Debug.Log("Saving prefab");
    17.             }
    18.              finally
    19.             {
    20.                 PrefabUtility.UnloadPrefabContents(prefabRoot);     // Unload to clear memory..              
    21.             }
    22.         }
    23.         else
    24.         {
    25.             RoomConfig currentConfig = FindObjectOfType<RoomConfig>();
    26.             currentConfig.AddPositionMarker(this);
    27.             EditorUtility.SetDirty(currentConfig);
    28.         }
    Now this works fine in the Prefab Editor. I can see the added reference in the public field of the RoomConfig. (Yes I made sure it's public and gets saved ^^)

    However when I instatiate the Prefab during Runtime I have this weird behaviour:
    It works the first time just fine. But when I start the Scene a second time the Prefab loses the added references.

    When I drag the prefab into the current scene the added fields get automatically flagged as "changed" by the Editor. (Bold font and listed in the override list).

    What am I doing wrong? Or is it a bug?
     
  2. Good_Punk

    Good_Punk

    Joined:
    Aug 6, 2014
    Posts:
    81
    Ok, found the answer myself. I needed to add the "EditorUtility.SetDirty" also to the Prefab Editor branch.