So far we have covered the Sequence object and the Effect object, but we’ve left out the third object that you can use to control your movement effects; the SequenceInstance object. In this section we’re going to use SequenceInstance extensively, so here are some basic facts about it:
You can’t create a SequenceInstance object directly, but every time you call Movement.Run one will be created for that particular running instance and returned to you. The SequenceInstance is a very powerful variable since it allows you to modify many of the fields that the effect is using while the effect is running.
There are also a couple of options exposed through the SequenceInstance that are not available to be set in either the Effect or Sequence. These are SequenceInstance.Loop and SequenceInstance.Timescale.
In order to save memory allocations SequenceInstance variables use an object pooling system. This has a lot of advantages when it comes to performance, but it also has one downside: If you hold on to a SequenceInstance variable after the sequence that it points to has finished running, then that SequenceInstance will eventually be recycled and you could end up changing values on a completely random sequence.
There are two ways that you can address this issue:
- You can cache the value of SequenceInstance.RecycleCount, and stop using the reference if that value is ever incremented.
- Or you can set SequenceInstance.ExcludeFromPooling to true, which will keep that particular SequenceInstance from being recycled. As long as you release your reference at some point the memory manager will come and delete the SequenceInstance eventually.