- Home /
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
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?
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.
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();
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.