- Home /
Moving/renaming folder
Hello.
Is there a way to change/rename folder with scripts for example and make Unity to load it properly when these scripts are attached to a Game Objects?
When I rename folder or move scripts to another I get "The associated script can not be loaded. [...]".
I do this in Unity and outside with moving .meta files too.
Thank you.
Hi, So you are just trying to rename a folder (which contains scripts) in your project window or in the system file browser? This should work fine, I recommend trying it with a new test project and seeing if you get the same results. Also, one question, are the game objects prefabs, that are dynamically loaded and not initially active in the scene?
Answer by Cliffinho · Mar 16, 2017 at 08:34 PM
Do operations with assets outside of Unity is not recomended, instead, using the class AssetDatabase, and ofcourse, you can do custom inspector/editor tools to improve the management of all kind assets (folders, animatior controllers, materials etc...). Look to AssetDatabase class, and see some classes to build editor tools: Editor Window, GUILayout, EditorGUILayout.
Look at this example script, he creates a window and list all folders of your project:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class FoldersListWindow : EditorWindow
{
private List<Folder> assetsFolders = new List<Folder>();
private Vector2 foldersListScroll;
[MenuItem("Window/FolderList %#o")]
private static void Init()
{
FoldersListWindow window = GetWindow<FoldersListWindow>();
window.titleContent = new GUIContent { text = "Folder List" };
window.Show();
}
private void OnEnable()
{
foreach (string folder in AssetDatabase.GetSubFolders("Assets"))
{
assetsFolders.Add(new Folder(folder));
}
}
private void OnGUI()
{
GUILayout.BeginArea(new Rect(1f, 1f, Screen.width / 3f, Screen.height), GUI.skin.box);
foldersListScroll = EditorGUILayout.BeginScrollView(foldersListScroll, true, true);
DrawFolders(assetsFolders);
EditorGUILayout.EndScrollView();
GUILayout.EndArea();
}
private void DrawFolders(List<Folder> foldersList)
{
for (int i = 0; i < foldersList.Count; i++)
{
EditorGUILayout.BeginVertical(GUILayout.Width(Screen.width / 3f));
foldersList[i].isFoldoutShown = EditorGUILayout.Foldout(foldersList[i].isFoldoutShown, foldersList[i].folderName);
if (foldersList[i].haveSubFolders && foldersList[i].isFoldoutShown)
{
EditorGUI.indentLevel++;
DrawFolders(foldersList[i].subFolders);
EditorGUI.indentLevel--;
}
EditorGUILayout.EndVertical();
}
}
}
// THIS CLASS IS USED TO REPRESENT A FOLDER (WITH SUB FOLDERS) IN THE ASSETS FOLDER
public class Folder
{
public string fullPath;
public string folderName;
public bool haveSubFolders;
public bool isFoldoutShown;
public int subFoldersCount;
public List<Folder> subFolders;
public Folder(string path)
{
fullPath = path;
folderName = GetOnlyFolderName(fullPath);
haveSubFolders = CheckForSubFolders(fullPath);
if (haveSubFolders)
{
subFolders = new List<Folder>();
string[] folders = AssetDatabase.GetSubFolders(fullPath);
foreach (string item in folders)
{
subFolders.Add(new Folder(item));
}
subFoldersCount = subFolders.Count;
isFoldoutShown = true;
}
else
{
haveSubFolders = false;
subFoldersCount = 0;
isFoldoutShown = false;
}
}
private bool CheckForSubFolders(string path)
{
string[] folders = AssetDatabase.GetSubFolders(path);
if (folders.Length > 0)
return true;
else
return false;
}
private string GetOnlyFolderName(string path)
{
return path.Substring(path.LastIndexOf('/') + 1);
}
}
Really a bit "complicated", but it gives you the power of manipulate all assets in a project.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113144im_/https://answers.unity.com/themes/thub/images/avi.jpg)