A journal button that works on every scene

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

A journal button that works on every scene

Post by AoF »

Hello! I'd like to create a Journal button that works regardless of what scene you're in, but I'm not sure how to do that.

Here's my setup right now:



The problem is, when I go to a new scene in play mode, the onclick events of my journal button say "Missing."



I think that's because the Dialogue UI is donotdestroy, but my journal button isn't, so in the next scene, the prefab is referring to something that's not there like it used to be.

What's the "right" way to fix this? I'm thinking the easiest might be to take the Quest Log out of the Dialogue UI and move it to a non-donotdestroy part of the hierarchy.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: A journal button that works on every scene

Post by Tony Li »

You can add a script like this to your UI Button and configure OnClick() to call its OpenQuestLogWindow() method:

Code: Select all

using UnityEngine;
public class QuestLogWindowOpener : MonoBehaviour
{
    public void OpenQuestLogWindow()
    {
        FindObjectOfType<PixelCrushers.DialogueSystem.QuestLogWindow>().Open();
    }
}
You can also find similar code in QuestLogWindowHotkey.cs, which binds a key to toggle the quest log window open and closed.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: A journal button that works on every scene

Post by AoF »

Ah. Is that performant? I never know with those Find methods.

In the end I decided to put my journal button inside the Dialogue Manager so it can always have a reference to the Quest Log field.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: A journal button that works on every scene

Post by Tony Li »

It's absolutely fine for a single use. You wouldn't want to use FindObjectOfType inside a loop that repeats every frame, such as an Update() method.

Even so, the QuestLogWindowHotkey script I mentioned above caches the FindObjectOfType result so it only needs to look it up once after changing to a new scene.
Post Reply