Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Scott Peal · Dec 24, 2010 at 03:38 AM · guiformdesigner

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");

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · Dec 25, 2010 at 07:52 AM

Try using AssetDatabase.ImportAsset(path); followed by AddComponent

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scott Peal · Dec 27, 2010 at 05:11 PM 0
Share

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"));

avatar image Mike 3 · Dec 27, 2010 at 07:45 PM 0
Share

No refresh, and newForm.AddComponent("ClassName"); not the path

avatar image Scott Peal · Dec 28, 2010 at 01:00 AM 0
Share

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.

avatar image Mike 3 · Dec 28, 2010 at 02:27 AM 1
Share

Remove the .cs from AddComponent

avatar image Fornoreason1000 · Nov 01, 2013 at 09:39 AM 0
Share

System.Reflection can also help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges