Using serialized properties/objects is very beneficial - but I always ran away from them in editor code cause I was never able to get a non-null/legit value out of FindRelativeProperty
#-_______-
Here’s what I have:
FSMTrigger.cs
public class FSMTrigger : BaseTrigger, Interaction
{
public List<FSMTriggerState> states = new List<FSMTriggerState>();
// stuff...
}
FSMTriggerState.cs
public class FSMTriggerState : MonoBehaviour
{
public List<int> ints = new List<int>() { 1, 2, 3, 4, 5 }; // for testing
public List<FSMTriggerTransition> transitions = new List<FSMTriggerTransition>();
public FSMStateType stateType = FSMStateType.Other;
}
FSMTriggerTransition.cs
[ExecuteInEditMode]
public class FSMTriggerTransition : MonoBehaviour
{
[HideInInspector] public List<EventDelegate> onTransition = new List<EventDelegate>();
public FSMTriggerState toState;
// stuff...
}
For the sake of completeness:
public abstract class BaseTrigger : MonoBehaviour
{
// stuff...
}
public interface Interaction
{
void Interact(UnityEngine.GameObject actor);
}
Here’s what I’m doing:
FSMTriggerEditor.cs
void Test()
{
var spStates = serializedObject.FindProperty("states");
for (int i = 0; i < spStates.arraySize; i++) {
Debug.Log("STATE TYPE: " + spStates.GetAt(i).GetValue<FSMTriggerState>().name); // <--- THIS WORKS
}
var ints = spStates.FindPropertyRelative("ints");
for (int i = 0; i < ints.arraySize; i++) {
Debug.Log("Numbers: " + ints.GetAt(i).GetValue<int>()); // THIS DOESN'T
}
var spTransitions = spStates.FindPropertyRelative("transitions");
for (int i = 0; i < spTransitions.arraySize; i++) {
Debug.Log("Transitions: " + ints.GetAt(i).GetValue<FSMTriggerTransition>().name); // NOR THIS
}
}
I wanted to make sure that the problem is NOT with the state class itself, so I went ahead and:
FSMTriggerStateEditor.cs
private void test()
{
var spTransitions = serializedObject.FindProperty("transitions");
for (int i = 0; i < spTransitions.arraySize; i++) {
Debug.Log("Transitions names: " + spTransitions.GetAt(i).GetValue<FSMTriggerTransition>().name); // THIS WORKED! SO AS THE INTS OF COURSE
}
}
So it’s pretty obvious that FindRelativeProperty
isn’t working right - maybe I just don’t know how to use it - but I’ve seen a lot of scripts just reference the thing like I did - there’s no special path - just the name of the variable I want to fetch. Yet it’s not working with me - I get null reference exceptions. Even if I try the simplest of examples
Any help would be greatly appreciated - thank you very much in advance.
EDIT: There was a mistake in the test script using FindRelativeProperty
- although I fixed that, it’s still giving me a null ref:
var spStates = serializedObject.FindProperty("states");
for (int i = 0; i < spStates.arraySize; i++) {
var state = spStates.GetAt(i);
Debug.Log("STATE TYPE: " + state.GetValue<FSMTriggerState>().name); // WORKS
var ints = state.FindPropertyRelative("ints");
for (int j = 0; j < ints.arraySize; j++) { // BREAKS HERE - MEANING INTS IS NULL
Debug.Log("Numbers: " + ints.GetAt(j).GetValue<int>());
}
}
EDIT: