- Home /
Custom Inspector saving variables to its self but not the edited script
I have a weaponItem class that I have been working on for a while its very simple you select an enum to pick the type of weapon and then you can select all the values based on the weapon type. I have spent a week building a custom inspector for it and it works perfectly in unity when editing, and in runtime. However It won't actually set the values in the script. I know this is more then likely a stupid over look on my end because otherwise you would think someone would have asked it already lol. I will post my code I know its going to look bad because I am a messy programmer when I am prototyping an idea in code so sorry for that. Please help me catch my mistake before I have to stop using a custom editor. This is the complete actual script I am working with no omissions or truncations please help me find my bug. Again everything saves in when looking at the inspector, the only way I knew it was setting anything in the script was debug logs showing the curRangeMax to hold a 2 when I had it set to 50. More debuging showed nothing was actually being set at all. If you need anything else to help me me solve this issue please let me know
The first one is an item script that must derive from another class Weapon
public class WeaponItem : Weapon {
//This is the location at which the raycast is generated
// NOTE: Must be places just in from of the barrel of the weapon
// and must be outside the gun collision object
//public Transform weaponRefference;
public string WeaponName;
//enum to select weapon type
public WeapType Type;
//********************* Range Variable ****************************
//Maximum ranges based on weapon type
public float
maxPisRange = 50f,
maxBowRange = 50f,
maxRifRange = 75f,
maxSniperRange = 150f,
curRangeMax = 2;
//********************** Damage Variables **************************
//damage of the current gun
public float curMinDmg;
public float curMaxDmg;
//Maximum ranges based on weapon type
public float
maxPisDamage = 4f,
maxBowDamage,
maxRifDamage,
maxSniperDamage;
//Minimum ranges based on weapon type
public float
minPisDamage = 1f,
minBowDamage,
minRifDamage,
minSniperDamage;
//********************** View Range Variables **************************
//View Range of current weapon
public float curViewRange = 2;
//Maximum view range based on weapon type
public float
maxPisView = 25,
maxBowView,
maxRifView,
maxSniperView;
//A list of enums
public List<AttributeObject> atrObject;
//*********************** Ammo Variables ***************************
//Maximum clip size based on weapon
public int
maxPisClip = 4,
maxBowClip = 1,
maxRifClip = 5,
maxSniperClip = 5;
public int
maxBeltSize = 150,
maxDrumSize = 300,
maxExtendSize = 100,
maxStandSize = 50;
//*********************** Projectile Variables ***************************
//Speed of the projectile in m/s
public float projectileSpeed;
//Number of shoots to fire when on burst mode
public int burstRate;
public bool useGravity;
//*********************** Weapon Feature Variables ***********************
public FireType ColType;
public FireMode FireSelect;
public DamageType TypeDamage;
public bool autoReload;
public float fireRate;
public GameObject projectilePrefab;
public Transform BulletSpawn, WeaponMount;
public GameObject mainCamera, BulletSpawnObject;// = new CameraObject();
public Camera cam;
//List holding different clips
public List<ClipObject> SelectClip;
//Holds the currently selected clip
public ClipObject curClip;
public LayerMask HitLayer;
public void OnEnable(){
InventoryItem = this.gameObject;
mainCamera = GameObject.FindWithTag("MainCamera") as GameObject;
BulletSpawnObject = GameObject.FindWithTag("ProjectileSpawn") as GameObject;
BulletSpawn = BulletSpawnObject.transform;
if(mainCamera == null)
Debug.Log ("Unable to find Soldier Camera!");
cam = mainCamera.camera;
}
}
void Update(){
//mainCamera = gameObject.GetComponent<CameraObject>();
}
public void ShootRay(){
bool overEnemy;// value showing if we are over and enemy or not
Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0f));
RaycastHit hit1;
RaycastHit hit2;
overEnemy = Physics.Raycast(ray.origin, ray.direction,out hit1, 100f,HitLayer);
Debug.Log (Type.ToString());
Debug.Log("Max Range: " + curRangeMax.ToString());
Debug.Log("Hit Distance: " + hit1.distance.ToString());
Debug.Log("Hit Target: " + hit1.collider.name);
Debug.Log("Damage Max: " + curMaxDmg.ToString() + "Min: " + curMinDmg.ToString());
if(overEnemy)
{
// if(Physics.Raycast(ray.origin, ray.direction,out hit2, curRangeMax, otherLayer))
// {
// overEnemy = hit1.distance < hit2.distance;
// enemyTarget = hit1.collider.gameObject;
// }
NGUIDebug.Log("Enemy Targeted");
}
else{
//enemyTarget = null;
NGUIDebug.Log("No Enemy Targeted");
}
}
}
This is the editor script
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
[CustomEditor(typeof(WeaponItem)),CanEditMultipleObjects]
public class WeaponItemEditor : Editor {
//ScriptObject for easy access to variables
WeaponItem weapon;
//Temporary Variables for min max sliders
float
tmpMinDmg = 0.2f,
tmpMaxDmg = 2f,
tmpMaxPisDmg,
tmpMinPisDmg;
public SerializedProperty bnsList;
public SerializedProperty clipList;
public SerializedProperty cam;
public void OnEnable(){
//Set the script object to the script on the object we are currently using.
weapon = (WeaponItem)target;
bnsList = serializedObject.FindProperty("atrObject");
clipList = serializedObject.FindProperty("SelectClip");
cam = serializedObject.FindProperty("mainCamera");
tmpMinPisDmg = (float)weapon.minPisDamage;
tmpMaxPisDmg = (float)weapon.maxPisDamage;
//Debug.Log(tmpMaxPisDmg + "__" + tmpMinPisDmg);
// if(cam == null)
// NGUIDebug.Log("Camera not being set in the editor");
// else
// weapon.mainCamera = cam;
//
// if(weapon.mainCamera == null)
// NGUIDebug.Log("editor not setting mainCamera in weaponitem");
}
public void Update(){
tmpMinPisDmg = weapon.minPisDamage;
tmpMaxPisDmg = weapon.maxPisDamage;
tmpMinDmg = weapon.curMinDmg;
tmpMaxDmg = weapon.curMaxDmg;
//weapon.mainCamera = cam;
}
//This method overrides the normal call to draw the gui and is were all the gui code goes.
public override void OnInspectorGUI()
{
GUI.changed = false;
serializedObject.Update ();
//weapon.BulletSpawn = (Transform)EditorGUILayout.ObjectField("Bullet Spawn Point",weapon.BulletSpawn,typeof(Transform),false);
//weapon.WeaponMount = (Transform)EditorGUILayout.ObjectField("Mount Location",weapon.WeaponMount,typeof(Transform),false);
//EditorGUILayout.LabelField("Camera Object");
//weapon.mainCamera = (Camera)EditorGUILayout.PropertyField(cam,true);
weapon.HitLayer = (LayerMask)EditorGUILayout.LayerField("Ignore Layer",weapon.HitLayer);
//Creates an info box this one gives instructions to the user
EditorGUILayout.HelpBox("Select the type of weapon to create",MessageType.Info);
EditorGUILayout.Separator();
EditorGUILayout.TextField("Name of the Weapon ",weapon.WeaponName);
EditorGUILayout.Separator();
EditorGUILayout.Separator();
//This sets up an enum selector so the user can select the type of weapon to create
weapon.Type = (WeapType)EditorGUILayout.EnumPopup(weapon.Type);
EditorGUILayout.Separator();
EditorGUILayout.Separator();
//Switch to create a custom layout related to the spacific weapon type
switch (weapon.Type){
//Pistol Inspector
case WeapType.PISTOL:
EditorGUILayout.Separator();
//Allow for the setting of a custom range
EditorGUILayout.LabelField("Set Maximum Effective Range Max: " + weapon.maxPisRange.ToString() + " | 1 unit = 1m");
weapon.curRangeMax = (float)EditorGUILayout.Slider(weapon.curRangeMax,1f,weapon.maxPisRange);
EditorGUILayout.Separator();
//Allow for the setting of a damage range
EditorGUILayout.LabelField("Set the Set the Dmg Range Between Min: " + tmpMinPisDmg.ToString() + " Max: " + tmpMaxPisDmg.ToString() );
tmpMinDmg = EditorGUILayout.FloatField("Min Dmg", tmpMinDmg);
tmpMaxDmg = EditorGUILayout.FloatField("Max Dmg", tmpMaxDmg);
EditorGUILayout.MinMaxSlider(ref tmpMinDmg,ref tmpMaxDmg,tmpMinPisDmg,tmpMaxPisDmg);
weapon.curMinDmg = tmpMinDmg;
weapon.curMaxDmg = tmpMaxDmg;
//Debug.Log("Current Dmg Min" + weapon.curMinDmg + " Current Dmg Max" + weapon.curMaxDmg);
EditorGUILayout.Space();
//Allow for the setting of a view range
EditorGUILayout.LabelField("Set the targeting view range Max: " + weapon.maxPisView.ToString() + " | 1 unit = 1m");
weapon.curViewRange = (float)EditorGUILayout.Slider(weapon.curViewRange,1f,weapon.maxPisView);
EditorGUILayout.Separator();
//Allow the addition of weapon bonuses
EditorGUILayout.LabelField("Add Bonuses and Resistances if any");
//SerializedProperty bonus = bnsList.
EditorGUILayout.PropertyField(bnsList,true);
EditorGUILayout.Separator();
//Allow for the selection of clip types
EditorGUILayout.LabelField("Create a list of allow clip styles Max: " + weapon.maxPisClip.ToString());
//SerializedProperty bonus = bnsList.
EditorGUILayout.PropertyField(clipList,true);
weapon.autoReload = EditorGUILayout.ToggleLeft("Do you want to auto reload?",weapon.autoReload);
EditorGUILayout.Separator();
//Select the type of Collision Detection to use
EditorGUILayout.LabelField("How Do you want to detect collisions");
weapon.ColType = (FireType)EditorGUILayout.EnumPopup(weapon.ColType);
EditorGUILayout.Separator();
//Select the fire mode for the gun NOTE: Make this so that we can offer many fire modes on one gun.
EditorGUILayout.LabelField("How will this weapon fire");
weapon.FireSelect = (FireMode)EditorGUILayout.EnumPopup(weapon.FireSelect);
EditorGUILayout.Separator();
break;
//Bow Inspector
case WeapType.BOW:
break;
//Riffle Inspector
case WeapType.RIFFLE:
break;
//Sniper Inspector
case WeapType.SNIPER:
break;
}
//Save all the changes to non serializedOjects
if (GUI.changed){
EditorUtility.SetDirty(target);}
//Save all the changes to serialized objects
serializedObject.ApplyModifiedProperties ();
}
}
Your answer

Follow this Question
Related Questions
Adding dropdown to a list add buttom 2 Answers
Calling a custom inspector from an editor window of an instance of a class 1 Answer
Custom Editor has stopped working 1 Answer
Custom tree-like editor 0 Answers
Custom Editor and OnSceneGUI 1 Answer