I make a script that can change values of ScriptableObjects. It takes a UnityEngine.Object (the ScriptableObject), a System.Object (the change) and a type that specifies which type gets changed. I have a simple editor script that says when the type is an integer it will create an EditorGUILayout.IntField(). When the type is a float it will create a FloatField… The problem is i can’t get the System.Object variable. I tried getting it with a SerializedProperty but its value was null. I tried getting it through the script but when i ran the game the value dropped to zero. Does anyone know how to fix this issue?
This is my Script.
using UnityEngine;
public enum VariableType
{
Int,
Float,
Bool,
String
}
public class VariableChange : MonoBehaviour
{
#region - Conditions -
//General
public enum ConditionType
{
Collision
}
public ConditionType conditionType;
//Collision
public string collisionTag;
#endregion
#region - VariableChange -
//General
public VariableType varType;
public Object variable;
public object change;
//Int/Float
public enum OperatorationType
{
Addition,
Subtraction,
Multiplication,
Division,
Equals
}
public OperatorationType operationType;
//Bool
public bool invertValue;
//String
//For Later
#endregion
void ChangeVariable()
{
switch (varType)
{
case VariableType.Int:
{
IntVar tempVar = (IntVar)variable;
switch (operationType)
{
case OperatorationType.Addition:
tempVar.value += (int)change;
break;
case OperatorationType.Subtraction:
tempVar.value -= (int)change;
break;
case OperatorationType.Multiplication:
tempVar.value *= (int)change;
break;
case OperatorationType.Division:
tempVar.value /= (int)change;
break;
case OperatorationType.Equals:
tempVar.value = (int)change;
break;
}
}
break;
case VariableType.Float:
{
FloatVar tempVar = (FloatVar)variable;
switch (operationType)
{
case OperatorationType.Addition:
tempVar.value += (float)change;
break;
case OperatorationType.Subtraction:
tempVar.value -= (float)change;
break;
case OperatorationType.Multiplication:
tempVar.value *= (float)change;
break;
case OperatorationType.Division:
tempVar.value /= (float)change;
break;
case OperatorationType.Equals:
tempVar.value = (float)change;
break;
}
}
break;
case VariableType.Bool:
{
BoolVar tempVar = (BoolVar)variable;
if (!invertValue)
tempVar.value = (bool)change;
if (invertValue)
tempVar.value = !tempVar.value;
}
break;
case VariableType.String:
{
//StringVar tempVar = (StringVar)variable;
//tempVar.value += (int)change;
}
break;
}
}
void OnTriggerEnter(Collider collision)
{
if (conditionType != ConditionType.Collision)
return;
if (collision.tag == collisionTag)
{
ChangeVariable();
}
}
}
This is the custom editor of it.
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(VariableChange))]
public class VariableChangeEditor : Editor
{
// - Conditions -
SerializedProperty conditionType;
//Collision
SerializedProperty collisionTag;
// - VariableChange -
SerializedProperty variable;
SerializedProperty varType;
//Int, Float
SerializedProperty operationType;
//Bool
SerializedProperty invertValue;
//String
//For Later
//Script
VariableChange script;
void OnEnable()
{
script = (VariableChange)target;
#region Conditions
//General
conditionType = serializedObject.FindProperty(nameof(conditionType));
//Collision
collisionTag = serializedObject.FindProperty(nameof(collisionTag));
#endregion
#region - VariableChange -
//General
variable = serializedObject.FindProperty(nameof(variable));
varType = serializedObject.FindProperty(nameof(varType));
script.change = new object();
//Int, Float
operationType = serializedObject.FindProperty(nameof(operationType));
//Bool
invertValue = serializedObject.FindProperty(nameof(invertValue));
//String
//For Later
#endregion
}
public override void OnInspectorGUI()
{
serializedObject.Update();
#region Conditions
EditorGUILayout.PropertyField(conditionType);
if (conditionType.enumValueIndex == (int)VariableChange.ConditionType.Collision)
{
EditorGUILayout.PropertyField(collisionTag);
}
#endregion
EditorGUILayout.Space();
#region - VariableChange -
EditorGUILayout.PropertyField(variable);
EditorGUILayout.PropertyField(varType);
switch (varType.enumValueIndex)
{
case (int)VariableType.Int:
{
if (script.change.GetType() != typeof(int))
script.change = 0;
script.change = EditorGUILayout.IntField("Change", (int)script.change);
EditorGUILayout.PropertyField(operationType);
}
break;
case (int)VariableType.Float:
{
if (script.change.GetType() != typeof(float))
script.change = 0f;
script.change = EditorGUILayout.FloatField("Change", (float)script.change);
EditorGUILayout.PropertyField(operationType);
}
break;
case (int)VariableType.Bool:
{
if (script.change.GetType() != typeof(bool))
script.change = false;
if (!invertValue.boolValue)
script.change = EditorGUILayout.Toggle("Change", (bool)script.change);
EditorGUILayout.PropertyField(invertValue);
}
break;
case (int)VariableType.String:
{
if (script.change.GetType() != typeof(string))
script.change = "";
script.change = EditorGUILayout.TextField("Change", (string)script.change);
//Note: Values for changing string changing procedure
}
break;
}
#endregion
serializedObject.ApplyModifiedProperties();
}
}
#endif