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 Sendatsu_Yoshimitsu · Aug 28, 2014 at 02:29 AM · c#guiinspector

Problem using EditorGUILayout.ObjectField with custom type

So I have a class Skill, which is Serializable, and I want to add an inspector slot I can drag subclasses of Skill onto in order to customize those items in edit mode. My class currently reads:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Skill {
     public string name;
     public int id;
     public float castTime;
     public float cooldown;
 
     public Skill(string skillName, int skillID, float skillCastTime, float skillCooldown){
         name = skillName;
         id = skillID;
         castTime = skillCastTime;
         cooldown = skillCooldown;
     }
 
 
     public void Use(GameObject npc){
 
     }
 }

I created a new variable of type Skill named itemSkill in my base item class, and in my database I'm trying to expose it with:

 Skill itemSkill = EditorGUILayout.ObjectField(item.skillFunction, typeof(Skill), false);

However this generates an error, "The best overloaded match for `UnityEditor.EditorGUILayout.ObjectField(UnityEngine.Object, System.Type, params UnityEngine.GUILayoutOption[])' has some invalid arguments". Is there an obvious syntax error, or am I not using EditorGUILayout properly?

Comment
Add comment · Show 6
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 AlwaysSunny · Aug 28, 2014 at 03:39 AM 0
Share

Try casting?

Skill itemSkill = (Skill)EditorGUILayout.ObjectField(item.skillFunction, typeof(Skill), false);

avatar image Sendatsu_Yoshimitsu · Aug 28, 2014 at 03:51 AM 0
Share

Hmm, that yields a different error, "best overloaded match has some invalid arguments", but the documentation says that it wants an object, a typecast, and the value of AllowSceneObjects, and I'm giving it all three.

avatar image AlwaysSunny · Aug 28, 2014 at 04:54 AM 0
Share

I never had much experience coding for the Editor, so I'm not super sure what to suggest. I seem to recall that your first argument [can? must?] also be on the left-hand of the assignment operator, like in this snippet I dug up below. Also, guess I used an as-cast, not an implicit.

 this$$anonymous$$ember.prefab = EditorGUILayout.ObjectField(this$$anonymous$$ember.prefab, typeof(GameObject), false) as GameObject;
avatar image AlwaysSunny · Aug 28, 2014 at 05:04 AM 0
Share

Wait a sec... It's possible ObjectFields are only for components...? How else would you assign to them in the Inspector? I hate forgetting stuff I learned a year ago!

avatar image Sendatsu_Yoshimitsu · Aug 28, 2014 at 07:45 AM 0
Share

If that's the case, is there a function that can be used to expose custom classes? There must be...

Show more comments

1 Reply

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

Answer by isador34 · Aug 28, 2014 at 08:36 AM

for my editor I use this code: public ItemManager itemManager;

 itemManager = EditorGUILayout.ObjectField(itemManager, typeof(ItemManager)) as ItemManager;
Comment
Add comment · Show 3 · 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 Sendatsu_Yoshimitsu · Aug 28, 2014 at 07:35 PM 0
Share

I'm afraid that still gives me the same overloaded method match has some invalid arguments error. :(

avatar image shinriyo_twitter · Nov 12, 2015 at 11:35 AM 0
Share

yes Because it should be "Object".

 UnityEngine.Object
avatar image shinriyo_twitter · Nov 12, 2015 at 12:10 PM 0
Share

$$anonymous$$y good sample is here.

 using UnityEngine;
 using UnityEditor;
 
 public class $$anonymous$$yEditor : EditorWindow
 {
     [System.Serializable] // needed.
     class Hoge
     {
         public bool boolHoge;
         public int intHoge;
         public string strHoge;
     }
 
     [SerializeField] // needed.
     Hoge[] hoges = new Hoge[]
     {
         new Hoge
         {
             boolHoge = false,
             intHoge = 1,
             strHoge = "2",
         },
     }
 
     void OnGUI ()
     {
         ScriptableObject target = this;
         SerializedObject so = new SerializedObject(target);
         // set the propery name.
         SerializedProperty stringsProperty = so.FindProperty("hoges");
         EditorGUILayout.PropertyField(stringsProperty, true);
     }
 }

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

26 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

Related Questions

Draw more EditorGUILayout components? 1 Answer

Erroe when resizing GUITextures in C# file 1 Answer

Is there any easy way to keep buttons inside of box? (c#) 1 Answer

C# GUI Destroy or Disable? 1 Answer

How can i show number of lives as image during gameplay 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