- Home /
Check if Script exists in Project (not already assigned to a GameObject in the Scene)
We have an editor script, let's call it ScriptA, which does these steps:
1) Auto-generates a new script, let's call it ScriptB, and place ScriptB into the Asset\Resources folder. This works as expected.
2) Call AssetDatabase.Refresh(); so the script shows up inside the project. This works as expected.
3) Adds ScriptB as a component to a GameObject.
Here lies the issue.
In step 3, we get an error message saying the file doesn't exist. We can see the file in the explorer and in the Unity3D's project resource folder. We can also drag and drop the newly created ScriptB onto the GameObject in the scene manually. So we know the new script is correctly formed. We can also open the new script in Visual Studios for editing.
So we thought to some type of UI locking loop (remembering this is an editor) as a step 2.5 which would keep checking if the script exists to the Unity Engine yet. Once true, the continue to step 3.
It would appear that the UnityEngine/Editor has to detect a new file in the resource folder, then import it we are guessing. And as ScriptA is moving so fast, this does not occur before we try to add ScriptB as a component to the gameObject.
Does anyone know what this command is? We tried Resource.Load and Resource.LoadAtPath but the asset is a script and not one of the others shown in the examples on the web.
BTW, if anyone is wondering what our editor does? We are working on making a Form Designer (aka graphical GUI designer).
Thanks in advanced for any help,
Scott Peal Global Chief Architect Company Automation LLC
GameObject newFormGameObject = new GameObject(NewFormName); newFormGameObject.transform.parent = FormsRoot.transform; newFormGameObject.tag = "guiForm";
string FormScriptsDirectory = "Assets/Resources";
string path = Path.Combine(FormScriptsDirectory, "TestScript.cs");
string FormCode = "using UnityEngine;" + Environment.NewLine +
"using System.Collections;" + Environment.NewLine +
"public class TestScript : MonoBehaviour" +
Environment.NewLine +
"{" +
"}";
using (StreamWriter sw = File.CreateText(path))
{
sw.Write(FormCode);
}
AssetDatabase.ImportAsset("TestScript.cs");
newFormGameObject.AddComponent("TestScript.cs");
Answer by Mike 3 · Dec 25, 2010 at 07:52 AM
Try using AssetDatabase.ImportAsset(path); followed by AddComponent
Thanks for the response. Unfortunately, it doesn't work either. Here is the error message:
Can't add component because class 'C:\Users\Scott Peal\Documents\New Unity Project\Assets\Resources\FormDesignerScripts\Forms\worksScript.cs' doesn't exist! UnityEngine.GameObject:AddComponent(String)...
Here's the code: //Add scripts to the gameobject AssetDatabase.ImportAsset(Path.Combine(path, NewFormName + "Script.cs")); AssetDatabase.Refresh();
newForm.AddComponent(Path.Combine(path, NewFormName + "Script.cs"));
No refresh, and newForm.AddComponent("ClassName"); not the path
Hi $$anonymous$$ike, I tried it like you suggested in the last post and still not working. If the refresh is not called the script doesn't show up in the project. I added a sample code in my original post for to show what I did as a test. Thanks again for the help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092357im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null reference exception[SOLVED] 1 Answer
GUI after pressed Destruction 2 Answers
GUI Button not working...but the the script is correct... 2 Answers
(4.6 UI) How to set up a window with x buttons, with a scroll bar? 1 Answer
Scrolling Text 1 Answer