Search Unity

Via script Inspector changed variable is default on build (like prefab)

Discussion in 'Scripting' started by MaZy, Aug 24, 2019.

  1. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
    Code (CSharp):
    1.  
    2. [InitializeOnLoad]
    3. public static class NetworkSceneObjectEditor
    4. {
    5.  
    6.     static NetworkSceneObjectEditor()
    7.     {
    8.         if(!EditorApplication.isPlaying)
    9.             EditorApplication.hierarchyChanged += OnHierarchyChanged;
    10.     }
    11.  
    12.     static void OnHierarchyChanged()
    13.     {
    14.         if (EditorApplication.isPlaying)
    15.             return;
    16.  
    17.         var all = Resources.FindObjectsOfTypeAll<NetworkSceneObject>();
    18.         var networkSceneObjects = all.Where(obj => (obj.hideFlags & HideFlags.HideInHierarchy) != HideFlags.HideInHierarchy);
    19.         //Debug.LogFormat("There are currently {0} GameObjects visible in the hierarchy.", networkSceneObjects.Count());
    20.  
    21.         int i = 0;
    22.         foreach (var o in networkSceneObjects)
    23.         {
    24.             var netObject = o.GetComponent<NetworkObject>();
    25.             if (netObject == null)
    26.             {
    27.                 Debug.Log("NetworkObject is missing! NetworkSceneObject will not work");
    28.                 continue;
    29.             }
    30.  
    31.             o.scenenNetId = i;
    32.             netObject.NetId = i;
    33.  
    34.             //Debug.Log($"{netObject.gameObject.name} {i}");
    35.             i++;
    36.         }
    37.     }
    38. }
    39.  
    With this script properties get changed if something is changed in the hierarchy like duplicating or adding new gameobjects. It is just increasing the id number.

    I have two problems here.
    1. If I am in playmode and I change to the scene with those gameObjects sometimes the values are reseted to the value which I edit myself in the inspector.
    2. If I make build version.. the ideas will be reseted to default value of the prefab (to 0).

    So I think I am missing there something like "Save this objects" or "Save this edit".

    Any ideas?