diff --git a/Assets/Fungus/Scripts/Components/SayDialog.cs b/Assets/Fungus/Scripts/Components/SayDialog.cs index bea77588..548ff5f8 100644 --- a/Assets/Fungus/Scripts/Components/SayDialog.cs +++ b/Assets/Fungus/Scripts/Components/SayDialog.cs @@ -5,6 +5,7 @@ using UnityEngine; using UnityEngine.UI; using System; using System.Collections; +using System.Collections.Generic; namespace Fungus { @@ -54,7 +55,23 @@ namespace Fungus protected StringSubstituter stringSubstituter = new StringSubstituter(); - protected Writer GetWriter() + // Cache active Say Dialogs to avoid expensive scene search + protected static List activeSayDialogs = new List(); + + protected void Awake() + { + if (!activeSayDialogs.Contains(this)) + { + activeSayDialogs.Add(this); + } + } + + protected void OnDestroy() + { + activeSayDialogs.Remove(this); + } + + protected Writer GetWriter() { if (writer != null) { @@ -200,8 +217,14 @@ namespace Fungus { if (ActiveSayDialog == null) { - // Use first Say Dialog found in the scene (if any) - SayDialog sd = GameObject.FindObjectOfType(); + SayDialog sd = null; + + // Use first active Say Dialog in the scene (if any) + if (activeSayDialogs.Count > 0) + { + sd = activeSayDialogs[0]; + } + if (sd != null) { ActiveSayDialog = sd;