Assign a Timeline to ControlTrack with C# without loosing the connection when the scene reloads

I’m trying to add a TimelineAsset to a ControllTrack with C# and it is working. However when I restart Unity or reload the scene the connection is lost. If I however add the same GameObject (with the PlayableDirector) manually via the Inspector then Unity will remember it. I’ve tried using
AssetDatabase.SaveAssets()
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), EditorSceneManager.GetActiveScene().path)
EditorApplication.ExecuteMenuItem(“File/Save Project”)
EditorUtility.SetDirty()
to try to force Unity to save the changes in the assets but it does not help. Saving manually from the Editor UI does not help either.

This is the code I use to set the GameObject with the PlayableDirector for the second timeline (called sequence) to be played from a ControlTrack in the first timeline (called profile). As I said this code works, the Source Game Object field in the Control Playable Asset looks like it is set properly in the Inspector and the animation (sequence) plays as expected but when I save the scene and reloads it, this connection is gone (Source Game Object field set to None), but if I set it manually (drag-n-drop the GameObject with the PlayableDirector for the second timeline to the field Source Game Object in the ControlTrack via the Inspector) and save the scene it works fine to reload the scene, nothing is lost, everything keeps working.

var profile = parentProfile.GetComponent<PlayableDirector>().playableAsset as TimelineAsset;
ControlTrack sequenceTrack = profile.CreateTrack<ControlTrack>(null, sequence.name);
var sequenceClip = sequenceTrack.CreateClip<ControlPlayableAsset>();
sequenceClip.displayName = sequence.name;
var controlAsset = (ControlPlayableAsset)sequenceClip.asset;
controlAsset.sourceGameObject.defaultValue = sequenceDirector.gameObject;

I’m using Unity 2019.1.14 and have tried it in 2019.2.9 but I get the same result in both versions. @seant_unity do you have an explanation and/or workaround for this?

Sorry for duplicating this question, but I was not able to tagg you @seant_unity in the Answers section (it just tags someone else called just sean instead). Feel free to keep your answers in the answers section or move it over here if it needs a more in-depth discussion.

Answered on Answers section. Correctly this time, I hope :wink: It tags me automatically on a reply.

https://answers.unity.com/questions/1679149/how-to-assign-a-timeline-to-a-controltrack-with-c.html?childToView=1679170#comment-1679170

1 Like

While I have your attention @seant_unity could you have a look at this question I have about accessing individual curves in a timeline clip over at How to get a particular curve from a TimelineClip - Questions & Answers - Unity Discussions

done

1 Like