• 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 timothy-eckstein · Jul 12, 2015 at 11:58 AM · stringmonoscript

UnityEditor namespace is invalid

I have already looked at many of the possible solutions here: http://answers.unity3d.com/questions/316805/unityeditor-namespace-not-found.html

The problem is that it doesn't quite solve the issue I'm experiencing.

What I am developing is a tool for animators who are not programmers and do not spend much time learning the subtle nuances of Unity. This tool allows them to create an event trigger in their game that can attach a specified script to an object and run it all without needing to understand Unity. I have had the program work so beautifully by allowing the user to select a script in an editor-window and have it stored in a game script like so:

 public UnityEditor.MonoScript script = null;

The problem is that this cannot exist within the assets folder when I try to build (or rather, when the animator builds their demo-reel). I have tried SO MANY workarounds. This is the current workaround that gets the closest to working...

I have the user write the name of the script they want to be attached, it is stored as a string as such:

 public string script = null;

Then later on I try to find and attach the script using this function:

 public void ScriptAtTime(ArrayList scripts, GameObject obj, float oldTime, float newTime) {
     // iterate thorugh all the scripts
     foreach (ScriptKey scriptKey in scripts) {
         // if the ScriptKey is keyed for this time then assign and run it
         if (scriptKey.time >= oldTime && scriptKey.time < newTime) {
             // only attach the script if it exists
             if (scriptKey.value != null) {
                 
                 // find the script
                 Component theScript = null;
                 
                 string debugString = "Found " + Resources.FindObjectsOfTypeAll<MonoBehaviour>().Length + " scripts. Names are: ";
                 bool first = true;
                 foreach (Component comp in Resources.FindObjectsOfTypeAll<MonoBehaviour>()) {
                     if (!first) {
                         first = false;
                     }
                     else {
                         debugString += ", ";
                     }
                     debugString += comp.name;
                 }
                 Debug.Log(debugString);
 
                 foreach (MonoBehaviour comp in Resources.FindObjectsOfTypeAll<MonoBehaviour>()) {
                     if (comp.name.Equals(scriptKey.value)) {
                         theScript = comp;
                     }
                 }
                 
                 // attach the script (if it was found)
                 if (theScript != null) {
                     obj.AddComponent(theScript.GetType());
                 }
             }
         }
     }
 }

The problem I am having is that it doesn't find scripts within the Assets folder or any of its sub-directories. Instead, it finds scene objects which have scripts attached to them. This is obviously not what I want. How can I find a script in the resources using its name and attach it to an object. Keep in mind that simply using the string itself

 obj.AddComponent("myScript");

As described here http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html is no longer valid. That would have been the perfect way to do this!

So I ask again, how can I find a script by its name in the resources and attach it to an object?

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

What is the maximum byte length of Application.ExternalCall? (and oh yea, it doesn't escape strings correctly) 3 Answers

High Score Name entry 1 Answer

How to tell if a string contains some specified text 6 Answers

Enemy spawner help with lengths 0 Answers

Return name of file only 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