Open quest log window via button click

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Eoran
Posts: 3
Joined: Mon Aug 14, 2023 11:25 pm

Open quest log window via button click

Post by Eoran »

Hi there, I'm very new to unity and coding in general so sorry if this is a silly question, but how do I open the quest log window with a button click? I tried the call open () method in the button's on click but nothing shows up when I play the game. The hotkey (J) works fine though.

Clicking the button does seem to register as even though the quest log window doesn't show up, it has stopped my player character from moving(but not turning around). I feel the solution to this should be really simple but I'm just not getting it after trying for a whole night. Any help would be appreciated!
Attachments
buttonclick.JPG
buttonclick.JPG (13.97 KiB) Viewed 250 times
User avatar
Tony Li
Posts: 22119
Joined: Thu Jul 18, 2013 1:27 pm

Re: Open quest log window via button click

Post by Tony Li »

Hi,

In your Button's OnClick() event, make sure to assign the instance of the quest log window that's in your scene or in the Dialogue Manager's Canvas, not the quest log window prefab. Since the prefab isn't in a scene, it isn't visible.

instanceNotPrefab.png
instanceNotPrefab.png (160.53 KiB) Viewed 234 times

If your quest log window is added to the scene by the Dialogue Manager's Instantiate Prefabs component:

instantiatePrefabs.png
instantiatePrefabs.png (155.61 KiB) Viewed 234 times

Then you'll need to either (1) remove it from Instantiate Prefabs and add an instance to the Dialogue Manager's Canvas:

addedInstance.png
addedInstance.png (21.98 KiB) Viewed 234 times

or (2) use a small script like this on your Button: (no need to configure OnClick() in this case)

QuestLogWindowButton .cs

Code: Select all

using UnityEngine;
using UnityEngine.UI;

namespace PixelCrushers.DialogueSystem
{
    [RequireComponent(typeof(Button))]
    public class QuestLogWindowButton : MonoBehaviour
    {

        [Tooltip("(Optional) Use this quest log window. If unassigned, will automatically find quest log window in scene. If you assign a window, assign a scene instance, not an uninstantiated prefab.")]
        public QuestLogWindow questLogWindow;

        public QuestLogWindow runtimeQuestLogWindow
        {
            get
            {
                if (questLogWindow == null) questLogWindow = GameObjectUtility.FindFirstObjectByType<QuestLogWindow>();
                return questLogWindow;
            }
        }

        private void Awake()
        {
            if (questLogWindow == null) questLogWindow = GameObjectUtility.FindFirstObjectByType<QuestLogWindow>();
        }

        private void Start()
        {
            GetComponent<Button>().onClick.AddListener(() =>
            {
                if (runtimeQuestLogWindow != null)
                { 
                    if (runtimeQuestLogWindow.isOpen) runtimeQuestLogWindow.Close(); 
                    else runtimeQuestLogWindow.Open();
                }
            });
        }

    }
}
One other note: The Dialogue Manager GameObject is a "Don't Destroy On Load" GameObject. This means it survives scene changes. Don't make inspector connections between Don't Destroy On Load GameObjects such as the Dialogue Manager and non-Don't Destroy On Load GameObjects. Use the script above instead. A deeper explanation is in this post.
Eoran
Posts: 3
Joined: Mon Aug 14, 2023 11:25 pm

Re: Open quest log window via button click

Post by Eoran »

Thank you! Both prefab instance and script worked great and I'll keep the side note about scene changes in mind moving forward.
User avatar
Tony Li
Posts: 22119
Joined: Thu Jul 18, 2013 1:27 pm

Re: Open quest log window via button click

Post by Tony Li »

Glad to help!
Post Reply