Slate Forums › Support › Avoiding null reference exceptions when changing scenes
I’m seeing a lot of errors getting thrown by Slate when a running cutscene is interrupted by changing scenes. It seems, from the error messages, that these are all the result of ‘actor’ being null during the OnUpdate call of a ActionClip. For example, LookAt.cs is encounters a NRE on the line `var dir = pos – actor.transform.position;’
Is there something I should be calling prior to loading a scene? Do I need to find all cutscenes in my scene and stop them before I load the new scene?
P.S: The forums don’t appear to allow .txt file attachments. I had to zip the text file I attached, as ZIP is allowed. Maybe allow .txt and .log?
So far it seems that the following code, run just before I unload the scene, appears to do the trick:
foreach (var slateCutscene in FindObjectsOfType<Cutscene>())
{
if (slateCutscene.isActive)
{
slateCutscene.Stop();
}
}
Hello again,
Indeed, you should make sure the the cutscenes are stopped before loading another scene. Your code should work fine and I guess that this should be handled automatically, but the problem is that there is no Unity callback that actually fires off before another scene is loaded (and before the objects are destroyed). I’ve tried SceneManager. sceneUnloaded and it doesn’t really fir the bill. There is also stop and cleanup code in the Cutscene.OnDestroy
, but as usual, OnDestroy in Unity, is called 1 frame after objects are actually destroyed.
As such I really think that what you are already doing is a good solution 🙂
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh
Sounds good. I appreciate the confirmation that my workaround isn’t a hack.
I’ve been working through some runtime crashes that users have been submitting for my game, and at this point a large fraction of those crashed are null reference exceptions throw during the OnDestroy of various Slate actions clips. Here’s an example:
<pre style=”box-sizing: border-box; overflow: auto; font-size: 13px; line-height: 1.42857; font-family: Menlo, Monaco, Consolas, ‘Courier New’, monospace; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; word-break: keep-all; overflow-wrap: break-word; background-color: #f5f5f5; border: 1px solid #cccccc; border-radius: 4px; white-space: pre-wrap;”>Slate.ActionClips.FinalIK.AnimateBipedLimb.OnUpdate (System.Single time) (at <6f0ca905826e4267a81d4771ab148b4b>:0)
Slate.ActionClip.OnUpdate (System.Single time, System.Single previousTime) (at <6f0ca905826e4267a81d4771ab148b4b>:0)
Slate.ActionClip.Slate.IDirectable.Update (System.Single time, System.Single previousTime) (at <6f0ca905826e4267a81d4771ab148b4b>:0)
Slate.StartTimePointer.Slate.IDirectableTimePointer.TriggerBackward (System.Single currentTime, System.Single previousTime) (at <6f0ca905826e4267a81d4771ab148b4b>:0)
Slate.Cutscene.Internal_SamplePointers (System.Single currentTime, System.Single previousTime) (at <6f0ca905826e4267a81d4771ab148b4b>:0)
UnityEngine.Debug:LogException(Exception)
Slate.Cutscene:Internal_SamplePointers(Single, Single)
Slate.Cutscene:Sample(Single)
Slate.Cutscene:Stop(StopMode)
Slate.Cutscene:OnDestroy()
What’s tricky about this is that I believe I’ve already calling Stop() on all cutscenes whenever I do something meaningful, like changing scenes, or anything else I can think of that would cause cutscenes to be destroyed.
What’s the reasoning for rewinding an active cutscene during OnDestroy? It seems pretty risky to me. I think the general cause of the exception is the same as I was seeing before: ‘actor’ tends to be null in the action clips. Do you have an example of what other problems might occur if a cutscene isn’t rewound during OnDestroy?
I’m not sure what else I can call to prevent Slate from generating errors. I’m also not sure whether these errors are legitimate, or whether they only occur as a side-effect of some other crash that brings down the application. In any case, at this point, I feel like I need to manually bullet-proof the slate action clips to avoid trying to access ‘actor’ when it’s null. But it would be really helpful if this was somehow automated.
Hello there,
There is an option on the cutscene component inspector called “Default Stop Mode”. This determines whether or not the cutscene will be rewinded, skipped to the end, or none of those (hold) when the cutscene stops, which can also be the result of calling Stop() manually.
What option do you have selected there for your cutscenes?
You can also force a specific StopMode when calling cutscene.Stop() by providing a StopMode parameter likeso:
cutscene.Stop(Cutscene.StopMode.Hold);
Can you please try setting this option to Hold, or alternatively call Stop with the parameter as shown above?
Please let me know.
Thank you.
Join us on Discord: https://discord.gg/97q2Rjh
Thanks. Right now, the Stop Mode on my cutscenes is set to “Skip”, which I guess is the default. I’ll set setting them to “Hold”.