I have an editor script that connects game objects up in the editor like so:
static void Editor_JoinWithCable()
{
var sel1 = UnityEditor.Selection.gameObjects[0];
var sel2 = UnityEditor.Selection.gameObjects[1];
DoorController dc = sel1.GetComponent<DoorController>();
Circuit cc = sel2.GetComponent<Circuit>();
var prefab = Resources.Load("Cable");
var cable = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefab);
Vector3 pos = (dc.transform.position + cc.transform.position) * 0.5f;
// cc.TriggeredObjects is: TriggerInput[]
var arr = cc.TriggeredObjects.ToList();
arr.Add(cable.GetComponent<TriggerInput>());
cc.TriggeredObjects = arr.ToArray();
// dc.Inputs is: TriggerInput[]
arr = dc.Inputs.ToList();
arr.Add(cable.GetComponent<TriggerInput>());
dc.Inputs = arr.ToArray();
UnityEditor.Selection.activeGameObject = cable;
UnityEditor.SceneView.RepaintAll();
}
It links up the objects as expected in the editor, but when I run the game, all the links are lost and I end up with null refs in the relevant arrays.
Only thing I can think is that Unity doesn’t like scripted links to components rather than game objects for some reason…? Or scene serialisation issues :-/