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
0
Question by slkjdfv · Apr 20, 2013 at 09:05 PM · editoreditorguiconversion

Assigning a script in custom editor

I'm trying to assign a script within a custom editor. Can this be done? And if not why haven't they allowed this? I've tried EditorGUI.ObjectField, PropertyField, and DragAndDrop. I keep getting this Error : Can not convert UnityEngine.Object to . I've even tried using casting and other conversion methods. Any help would be greatly appreciated. Thanks :D

Comment
Add comment · Show 5
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 numberkruncher · Apr 20, 2013 at 10:39 PM 0
Share

Can you explain in greater detail what you are hoping to achieve. Do you simply want to add a custom $$anonymous$$onoBehaviour component to a game object from an editor script?

You can select a script using an object field. The following page from the user guide explains how to add a component given a specific $$anonymous$$onoScript: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoScript.GetClass.html

avatar image slkjdfv · Apr 21, 2013 at 02:48 AM 0
Share

I have a class variable in my script. I want to be able to drag a script that derives from that class and assign it to that variable using the editor.

avatar image slkjdfv · Apr 21, 2013 at 07:26 PM 0
Share

Let me make this clearer. I can't use EditorGUILayout.ObjectField. Cause it's a script I'm referencing. $$anonymous$$onoScript looks to be the way to go but how can I convert it to my script? I can get the type but when I try to convert it I can't. It says 'cannot convert $$anonymous$$yScript to $$anonymous$$onoScript'. The script variable I have is just a basic class, it doesn't inherit from any thing.

avatar image numberkruncher · Apr 21, 2013 at 08:05 PM 0
Share

Afaik you can only select a script with $$anonymous$$onoScript if it inherits from UnityEngine.Object.

avatar image numberkruncher · Apr 21, 2013 at 08:08 PM 0
Share

The easier way would be just to use a text field and input the name of the class (including namespace).

A slightly more involved approach would be to create a popup menu and use reflection to display a list of selectable classes. You could filter the list using a custom interface or attribute:

 [SelectableIn$$anonymous$$yField]
 public class $$anonymous$$yNonObjectClass {
     ...
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ricardo_arango · Apr 21, 2013 at 03:56 AM

You would do something like this:

 MyClass obj;
 void OnGUI(){
    obj= EditorGUILayout.ObjectField(obj, typeof(MyClass), true) as MyClass;
 }

Note that I used EditorGUILayout.ObjectField for simplicity, the usage is similar for EditorGUI.ObjectField.

You can find more information in the documentation:

http://docs.unity3d.com/Documentation/ScriptReference/EditorGUILayout.ObjectField.html http://docs.unity3d.com/Documentation/ScriptReference/EditorGUI.ObjectField.html

Comment
Add comment · Show 1 · 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 iwillbenice · Nov 28, 2016 at 10:32 PM 0
Share

Works for me. Thanks

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

14 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Drawing Editor Inspector GUI based on selected/current prefab (CustomPropertyDrawer) 2 Answers

Custom Inspector Variables 1 Answer

Custom Editor fields listed below script variables 1 Answer

Prompting dialogue box for input in Editor? 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