- Home /
'FindPropertyRelative' and an instantiated class inside an instantiated class
I have a problem with comprehending custom drawers and 'FindPropertyRelative' method. For starters, I have these 4 classes:
SectionClass.cs
using UnityEngine;
using System;
[System.Serializable]
public class SectionData : System.Object
{
[SerializeField]
public int ObstacleType;
[SerializeField]
public Vector3Serializer ObstaclePosition = new Vector3Serializer();
public SectionData()
{
this.ObstacleType = 0;
this.ObstaclePosition.Fill(Vector3.zero);
}
public SectionData(int ObstacleType, Vector3 ObstaclePosition)
{
this.ObstacleType = ObstacleType;
this.ObstaclePosition.Fill(ObstaclePosition);
}
}
[System.Serializable]
public class Vector3Serializer : System.Object
{
[SerializeField]
public float x;
[SerializeField]
public float y;
[SerializeField]
public float z;
public void Fill(Vector3 v3)
{
this.x = v3.x;
this.y = v3.y;
this.z = v3.z;
}
public Vector3 V3 { get { return new Vector3(x, y, z); } set { Fill(value); } }
}
Test.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Test : CustomFunctions
{
public List<SectionData> testList = new List<SectionData>();
void Update ()
{
//Do Stuff
}
}
SectionCustomEditor.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor (typeof(Test))]
[CanEditMultipleObjects]
public class SectionCustomEditor : Editor
{
SerializedObject obj;
SerializedProperty testListProp;
void OnEnable ()
{
obj = new SerializedObject(target);
testListProp = obj.FindProperty("testList");
}
public override void OnInspectorGUI()
{
obj.Update();
for (int i = 0; i < testListProp.arraySize; i++)
{
SerializedProperty section = testListProp.GetArrayElementAtIndex(i);
SerializedProperty otpProp = section.FindPropertyRelative("ObstacleType");
//This bellow part is the problem and always returns null
SerializedProperty v3Prop = section.FindPropertyRelative("ObstaclePosition");
otpProp.intValue = EditorGUILayout.IntField ("Obstacle type: ", otpProp.intValue);
}
}
}
The problem is I can't understand how to access the whole 'Vector3Serializer' class and it's fields 'x', 'y', 'z'.
Comment
On line 25 of SectionCustomEditor.cs, what's that 'watProp' you're referencing?
@superpig whoops. Forgot to rename that, so the is more readable. Fixed the main post.
Your answer