- Home /
unity editor: serializing a vector2
so i just started exploring editor scripting and i wanted to create an editor script that checks if a specific Enum state is selected in a monobehavior script then shows its variables like speed, force....etc, so that the inspector shows the variables of this state only its more of an organization scripting of enums, let me illustrate this:
this is a normal script which has many enum states
now the script has 2 variables, pointB and speed, i want only to show those variables when the platform state is selected like this:
so i've writting a script that does that, its going really good with int values but it doesn't seem to work with Vector2 values in this case pointB variable.
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Objects)), CanEditMultipleObjects]
public class Changer : Editor
{
public SerializedProperty platformType, Rate, PointB;
void OnEnable()
{
platformType = serializedObject.FindProperty ("Type");
Rate = serializedObject.FindProperty ("Speed");
PointB = serializedObject.FindProperty ("PointB");
}
public override void OnInspectorGUI()
{
serializedObject.Update ();
EditorGUILayout.PropertyField (platformType);
Objects.objectsType st = (Objects.objectsType)platformType.enumValueIndex;
switch (st)
{
case Objects.objectsType.Platform:
EditorGUILayout.IntSlider (Rate, 0, 10, new GUIContent("MovingRate") );
EditorGUILayout.Vector2Field("Point 2", PointB); //does not work
break;
}
serializedObject.ApplyModifiedProperties ();
}
}
i get this error "Assets/Editor/Changer.cs(29,41): error CS1503: Argument #2' cannot convert
UnityEditor.SerializedProperty' expression to type UnityEngine.Vector2'" and this "Assets/Editor/Changer.cs(29,41): error CS1502: The best overloaded method match for
UnityEditor.EditorGUILayout.Vector2Field(string, UnityEngine.Vector2, params UnityEngine.GUILayoutOption[])' has some invalid arguments "
so what should i do ? and if something is not clear to you just ask. thanks !
Answer by vexe · May 01, 2015 at 03:37 AM
Just use PropertyField
like you did with platformType
- Vector2Field
expects a Vector2, not a SerializedProperty
switch (st)
{
case Objects.objectsType.Platform:
EditorGUILayout.PropertyField(Rate);
EditorGUILayout.PropertyField(PointB);
break;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613193948im_/https://answers.unity.com/themes/thub/images/avi.jpg)