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 EvandroLins · Jan 26, 2013 at 03:11 PM · c#monobehavioureditorguilayoutobjectfield

How make EditorGUILayout.ObjectField get a Monobehaviour from the Assets folder

In a custom editor, I want to let the user select a monobehaviour script from the Assets folder. Unfortunately, I'm not having much luck.

The code I'm using is:

  script = EditorGUILayout.ObjectField(script, typeof(MonoBehaviour));

It creates the field, but only let me select GameObjects in the scene.

So, anyone knows how to use the ObjectField to select a monobehaviour from the assets?

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
4
Best Answer

Answer by Bunny83 · Jan 27, 2013 at 03:17 AM

A MonoBehaviour script asset is not the same as the MonoBehaviour component. What you need is the MonoScript class. The MonoScript class is basically just a TextAsset which contains the source code, but in addition it has the GetClass function which can be used to access the "MonoBehaviour-type" that is implemented by this script. This type can be used in AddComponent for example.

Keep in mind that MonoScript is an editor class, so don't use it in runtime classes.

Comment
Add comment · Show 8 · 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 EvandroLins · Feb 19, 2013 at 04:30 AM 0
Share

Thanks, It worked \o/

avatar image Urre · Mar 26, 2014 at 07:04 PM 0
Share

Hi! How did you get this working? I can get it to find $$anonymous$$onoscript assets just fine, but how do you convert the type GetClass returns into a $$anonymous$$onobehaviour?

avatar image Urre · Mar 26, 2014 at 07:21 PM 0
Share

Ok I got it working when I used Object for variable type ins$$anonymous$$d of $$anonymous$$onoBehaviour

avatar image Bunny83 · Mar 26, 2014 at 07:33 PM 0
Share

@Urre: I'm not sure what you mean a $$anonymous$$onoScript is not an instance of that script, so you can't somehow "convert" it into a $$anonymous$$onoBehaviour. What you can do is using the type you get from GetClass in an AddComponent call. Like this:

     //C# (somewhere inside an editor script)
     $$anonymous$$onoScript someScriptReference;
     // [...]
     System.Type monoBehaviourType = someScriptReference.GetClass();
     someGameObject.AddComponent(monoBehaviourType);
avatar image Urre · Mar 26, 2014 at 11:08 PM 0
Share

Yeah, thing is I'm not looking to add it to a GameObject, I can't even see why that would be interesting. What I'm trying to do is make a custom inspector which can list scripts in my project when I click on the Object Picker icon, so, using ObjectField like in the above example, I need to somehow look for $$anonymous$$onoScripts in the project, but then get the value as a $$anonymous$$onoBehaviour. I've since I posted that one figured out you can't even assign scripts by drag & drop to a public $$anonymous$$onobehaviour variable for some reason, so I'm probably going entirely in the wrong direction. The goal is in the end to use FindObjectsOfType(sometype) with sometype being a class type defined by the user in the inspector, but I can't figure that out right now.

Show more comments

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Custom Editor: Objectfield clears on play 2 Answers

EditorGUILayout.Knob() !? 0 Answers

Access EditorGUILayout objects 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