title: how can i create a new layer using a script in unity
body: <p>i need function that goes through all the existing layer names and find if my layer is there and if not then the code need to add the given layer to the layer list</p>
  
  <p>i tried this code this <a href="https://forum.unity.com/threads/adding-layer-by-script.41970/" rel="nofollow noreferrer">unity forum</a></p>
  
  <pre><code>    void CreateLayer()
  {
      SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
  
      SerializedProperty layers = tagManager.FindProperty("layers");
      if (layers == null || !layers.isArray)
      {
          Debug.LogWarning("Can't set up the layers.  It's possible the format of the layers and tags data has changed in this version of Unity.");
          Debug.LogWarning("Layers is null: " + (layers == null));
          return;
      }
  
      for (int i = 8; i &lt; 31; i++)
      {
          SerializedProperty layerSP = layers.GetArrayElementAtIndex(i);
          if (layerSP.stringValue == "MyLayer")
          {
              return;
          }
      }
  
      for (int i = 8; i &lt; 31; i++)
      {
          SerializedProperty layerSP = layers.GetArrayElementAtIndex(i);
          if (layerSP.stringValue != "")
          {
              layerSP.stringValue = "MyLayer";
              break;
          }
      }
  
      tagManager.ApplyModifiedProperties();
  }
  </code></pre>
  
  <p>but it dosen't work for me</p>
  
  <p>by the way i'm using unity 5.6.3</p>
  
  <p>if anybody knows the answer please share</p>