a previous answer i found on here suggested i use UnityEditor package (find all root objects - Adrians aswer)
but now trying to do a build for my android device, i’m getting this eror :
“Assets/Controller.cs(5,7): error CS0246: The type or namespace name `UnityEditor’ could not be found. Are you missing a using directive or an assembly reference?”
Yes, the UnityEditor namespace is only available in the Editor. If you try to use the UnityEditor namespace in a regular script it will work in the editor but you’ll get an error when you try to build your project.
Besides putting scripts using the UnityEditor namespace into an Editor folder (any folder named “Editor” except inside other special folders like “Plugins” or “Standard Assets” where it has to be a direct sub-folder), you can use conditional compilation:
#if UNITY_EDITOR
using UnityEditor;
#endif
class Example : MonoBehaviour
{
void Start()
{
#if UNITY_EDITOR
// Put editor code here
#endif
}
}
Note also that classes in your Editor folders are compiled after your regular scripts, meaning regular scripts can’t access editor scripts.
Though the outcome is always the same: You can’t use any editor classes in your builds.
The only option you have is to work around not having the editor classes available at build. Regarding the original answer, you could create an editor script that uses the PostProcessScene attribute and gets all scene root objects when the scene is built in the editor and saves them on a script in the scene. Then you can get that script in the build and access its list of root objects (though that list will not be up to date if you instantiate new root objects into the scene).
(I did something similar to save the name of all scenes in a build, an information you can usually only access using editor classes, whereas you only get the name of the current scene in builds.)
For this case, it’s probably easier to create a root game object yourself, make sure you instantiate all objects as children of this root, then get it by name/tag/whatever in the build and iterate over its children.
All editor scripts (Scripts using the UnityEditor package or referencing a script who does) must be placed in a folder somewhere in the Assets folder named “Editor”.
Keep in mind that these scripts will not be built out into a build and is therefore not usable in a build at runtime.