Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Jonesy19 · Oct 07, 2016 at 07:15 PM · gameobjectscript.typeaddcomponent

Creating a Script at runtime, and adding it to an object...

Hi everyone, I'm using an editorscript to create scripts programmatically (in the editor, not a game). Now, I also am building up a level with this editor script and will need to add the newly created script to a gameobject. Is there any way to do this?
I am well aware that you can still addcomponent with a string by doing something like this:

 System.Type t = System.Type.GetType(scriptName);
 gameObj.AddComponent<t>();

But the script that I just generated is not in the list of types until I stop the editorscript and run it again. I'm guessing that the types get loaded on compile time, but is there a ways to "refresh" the System.Types during runtime, just like you can do an AssetDatabase.Refresh() at runtime?

Thanks!

Comment
Add comment · Show 2
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 Sergio7888 · Oct 08, 2016 at 12:56 PM 0
Share

Use:


System.Type t = System.Type.GetType(scriptName);
Component c = gameObj.AddComponent(t);
Scripts are asset but you cant load then at runtime, only in editor mode. In this case AssetDatabase.Refresh() should work.
avatar image Jonesy19 Sergio7888 · Oct 12, 2016 at 03:45 PM 0
Share

Thanks for the response, but as I mentioned in my post, AssetDatabase.Refresh doesn't work. :(

I did find the following, which highlights the issue. I'm currently working to see if I can figure out a solution that works for my situation...

http://stackoverflow.com/questions/23795173/notification-when-assetdatabase-refresh-is-ready

Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jonesy19 · Oct 13, 2016 at 04:00 PM

In case anyone is wondering, I got around my solution by doing the following (from the link in my comment above. I created a function and added [DidReloadScripts] to the top. That function will be run whenever the scripts are reloaded. For my purposes, that worked.

 [DidReloadScripts]
     public static void addComponentOnReload()
     {
         Debug.Log("Reloading Scripts...");
         GameObject obj = GameObject.Find("LevelController");
         System.Type t = System.Type.GetType("xyz");
         Component c = obj.GetComponent<xyz>();
         if (c == null)
         {
             c = obj.AddComponent(t);
             Debug.Log("Added Component Event_sheet_1 to LevelController...");
         }
     }

Comment
Add comment · 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

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

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cant add a script as component. Type is derived by base class 2 Answers

Can't add component because class doesnt exist 1 Answer

How to create gameobject with different setting 1 Answer

Hi im trying to set a Gameobject variable in a script not in the inspector. 3 Answers

GO.AddComponent just adds an empty Script 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