Page 1 of 1

Quest Control on Scene Load

Posted: Sat Feb 22, 2025 5:15 am
by Don Angel
I have 2 scenes, a "town" where you get the quest and a "forest" where you'll complete the quest.

I have it set up so that the quests are persistent between scenes.

What I want to do is that when you go to the "Forest" scene, if you have the FindCoins quest active, it sets to activa a game object with the coins as childs.

Right now, I have a cube in the scene that does that when the player collides with it.
But I'd like it to check if the quest is active when loading the scene.

Re: Quest Control on Scene Load

Posted: Sat Feb 22, 2025 8:21 am
by Tony Li
Hi,

Make sure you're changing scenes using the save system. See: How To: Change Scenes With Save System

Then you can add a script with a Start() method that checks the quest state. Example:

Code: Select all

using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using PixelCrushers;
using PixelCrushers.QuestMachine;
public class QuestEventOnStart : MonoBehaviour
{
    public Quest quest;
    public UnityEvent onQuestActive;
    
    IEnumerator Start()
    {
        for (int i = 0; i < SaveSystem.framesToWaitBeforeApplyData; i++) { yield return null; }
        yield return new WaitForEndOfFrame();
        if (QuestMachine.GetQuestState(quest.id) == QuestState.Active)
        {
            onQuestActive.Invoke();
        }
    }
}
Then add this to a GameObject (e.g., empty GameObject) in the Forest scene, assign the quest, and configure the On Start () event.