[SOLVED] Activate GameObject in another scene

Announcements, support questions, and discussion for Quest Machine.
Post Reply
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

[SOLVED] Activate GameObject in another scene

Post by mschoenhals »

Hi,
I'm trying to setup a Quest that includes a GameObject (NPC) in another scene that is Activated only if the player has the quest. I saw a short post about using a persistent GameObject but isn't Quest Machine already a persistent GameObject? Is the persistent GameObject in the Quest Giver scene or the scene with the triggered GameObject (NPC)?

Is it possible to disable that GameObject after the player has left that scene? What I'm trying to do is enable an NPC in one scene if the player get's a particular Quest, have the player finish the Quest in that scene, then have that NPC appear back in the original Quest Giver scene. Probably sounds confusing...

Thanks in advance. :D
Last edited by mschoenhals on Wed Feb 05, 2020 9:10 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Activate GameObject in another scene

Post by Tony Li »

Hi,

Unless you've set this NPC to "Don't Destroy On Load", it will only exist in the scene(s) it is in at design time. A common approach is to add a separate instance of the NPC in each scene. In each scene, only activate the NPC's GameObject if it's appropriate for the current state of the quest.

If you're using the Dialogue System in addition to Quest Machine, you can add a PersistentActiveData component to the NPC, or to a separate GameObject if the NPC GameObject starts inactive in the scene. Tick Check On Start, and set the Condition section to check the quest is in the required state to show the NPC.

If you're not using the Dialogue System, you can add a small script (or PlayMaker FSM, etc.) that checks the quest state and sets the NPC active/inactive accordingly.
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Activate GameObject in another scene

Post by mschoenhals »

I am going to use the Dialogue System - but I'm implementing the Quest System first. I'd like to use a script, how do I check the state of a particular quest then?
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Activate GameObject in another scene

Post by Tony Li »

Hi,

If your scene has only one QuestJournal (e.g., on the player), check QuestMachine.GetQuestState("quest ID").

If your scene has multiple QuestJournals (e.g., multiplayer), get a player's QuestJournal component and use FindQuest("quest ID").GetState().
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Activate GameObject in another scene

Post by mschoenhals »

I think I'm getting some of the syntax wrong. I do have only one Quest Journal (single player game). I'm trying to see the enum value of the Quest State:

Code: Select all

        if (QuestMachine.GetQuestState("rescueEzmore") == Successful)
        {
            print("Rescue Ezmore is in Successful State");
        }
However, it doesn't seem to know what "Successful" is.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Activate GameObject in another scene

Post by Tony Li »

Make sure this line is near the top of your script:

Code: Select all

using PixelCrushers.QuestMachine;
And change the code to:

Code: Select all

        if (QuestMachine.GetQuestState("rescueEzmore") == QuestState.Successful)
        {
            print("Rescue Ezmore is in Successful State");
        }
Tip: If you're using Visual Studio (VS), you can usually right-click on any error that VS has underlined in red. This will open a context menu. Select the first item in the context menu to get some options how to automatically fix it. In the case above, the context menu would offer to change "Successful" to "QuestState.Successful".
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Activate GameObject in another scene

Post by mschoenhals »

Thanks Tony. That worked really well. I don't have the context menu on my version of Visual Studio. I'm going to check it out and see if I can get it installed. Great tip! Thanks.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: [SOLVED] Activate GameObject in another scene

Post by Tony Li »

Glad to help!
Post Reply