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 vzdemon · Jun 07, 2012 at 08:33 AM · c#editorobject

C# Custom Object Editor Error

Hi, i have this object i created in C# called Skill. this is the object :

 public class Skill {
     string Name;
     float Effect;
     float CastTime;
     SkillType Type;
     float Cost;
     int Times;
     float Range;
     int Thumbnail;
     EffectProperties Ep;
 
     public Skill(string Name, float Effect, float CastTime, SkillType Type, float Cost, int Times, float Range, int Thumbnail, EffectProperties Ep){
         this.Name = Name;
         this.Effect = Effect;
         this.CastTime = CastTime;
         this.Type = Type;
         this.Cost = Cost;
         this.Times = Times;
         this.Range = Range;
         this.Thumbnail = Thumbnail;
         this.Ep = Ep;
     }
 
     public string namE {
         get{ return Name; }
         set{ Name = value; }
     }
 
 public float effect {
     get{ return Effect; }
     set{ Effect = value; }
 }
 
 public float castTime {
     get{ return CastTime; }
     set{ CastTime = value; }
 }
 
 public SkillType type {
     get{ return Type; }
     set{ Type = value; }
 }
 
 public float cost {
     get{ return Cost; }
     set{ Cost = value; }
 }
 
 public int times {
     get{ return Times; }
     set{ Times = value; }
 }
 
 public float range {
     get{ return Range; }
     set{ Range = value; }
 }
 
 public int thumbnail {
     get{ return Thumbnail; }
     set{ Thumbnail = value; }
 }
 
 public EffectProperties ep{
     get{ return Ep; }
     set{ Ep = value; }
 }
 }

 public struct EffectProperties {
 private bool atTarget;
 private bool projectile;
 private int particles;
 private float lifeTime;
 
 public EffectProperties(bool atTar, bool Proj, int Par, float lifeT){
     atTarget = atTar;
     projectile = Proj;
     particles = Par;
     lifeTime = lifeT;
 }
 
 public bool AtTarget{
     get{ return atTarget; }
     set{ atTarget = value; }
 }
 
 public bool Projectile{
     get{ return projectile; }
     set{ projectile = value; }
 }
 
 public int Particles{
     get{ return particles; }
     set{ particles = value; }
 }
 
 public float LifeTime{
     get{ return lifeTime; }
     set{ lifeTime = value; }
 }
 }

 public enum SkillType{
     passive,
     offensive
 }

and i have made an editor script for it so i can see the object in the inspector when ever i type public Skill myVar; int a script. but the problem is that i keep getting an error from the SkillEditor.cs script that says error CS0030: Cannot convert type UnityEngine.Object' to Skill' and i dont know how to solve it. please help, here is my SkillEditor.cs script:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;

 [CustomEditor (typeof(Stats))]
 public class SkillEditor : Editor {
     public override void OnInspectorGUI(){
         Skill pp = (Skill)target;
         GUILayout.Label("Skill Object Editor");
         pp.castTime = EditorGUILayout.FloatField("Cast Time",pp.castTime);
     }    
 }

thanks for reading

Comment
Add comment · Show 3
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 CHPedersen · Jun 07, 2012 at 09:16 AM 1
Share

I've never tried to get the editor to display custom objects, but the error suggests that the objects it displays in the editor must be of type UnityEngine.Object, and it has trouble displaying yours, because your class is not of that type. (It's of type System.Object).

Try to set your class to inherit from UnityEngine.Object and see what happens. That is, change the class declaration to this:

public class Skill : UnityEngine.Object

Does that change anything?

avatar image whydoidoit · Jun 07, 2012 at 11:57 AM 1
Share

If your custom class is serializable then it will be displayed in the inspector by default - no need for a custom script. However, you are using properties (not sure why as they don't seem to do much) and I believe the inspector only shows field values.

avatar image vzdemon · Jun 08, 2012 at 11:45 AM 0
Share

Thanks sooo much everyone works perfectly.

2 Replies

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

Answer by bompi88 · Jun 07, 2012 at 10:49 AM

I have never tried this, but you could try it out.

 // Creates new serializedObject from the selected gameobject 
 var so : SerializedObject = new SerializedObject(Selection.activeGameObject.GetComponent(Skill));
 
 so.FindProperty("castTime").floatValue = EditorGUILayout.FloatField("Cast Time", so.FindProperty("castTime").floatValue);
 
 so.ApplyModifiedProperties();
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
avatar image
1

Answer by Bunny83 · Jun 07, 2012 at 12:30 PM

The main issue is that your custom editor is for the class "Stats", so the target variable will always be a Stats instance which of course can't be casted to Skill.

Also keep in mind that structs aren't serialized! Also since you use private variables (and properties to access them) you need to mark the private variables with SerializeField so they are serialized.

 // ...
 [SerializeField]
 private string Name;
 // ...

Otherwise your editor is pretty useless since all changes are gone when you save / load the scene or you enter / leave playmode.

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

7 People are following this question.

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

Related Questions

How to move an object in world with script 1 Answer

Saving class fields in the Asset 0 Answers

How do I use EditorGUIUtility.ShowObjectPicker()? C# 3 Answers

Confused about one part of inheriting function from another objects script 2 Answers

Multiple Cars not working 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