Page 1 of 1

A journal button that works on every scene

Posted: Sat Jun 01, 2019 3:13 pm
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.

Re: A journal button that works on every scene

Posted: Mon Jun 03, 2019 11:10 am
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.

Re: A journal button that works on every scene

Posted: Mon Jun 03, 2019 1:42 pm
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.

Re: A journal button that works on every scene

Posted: Mon Jun 03, 2019 2:04 pm
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.