Save System Between Scenes
-
- Posts: 54
- Joined: Mon Jun 21, 2021 7:48 pm
Re: Save System Between Scenes
Awesome, just put it together on how it could work.
Thanks Tony, I'll probably be back with more questions eventually!
Thanks Tony, I'll probably be back with more questions eventually!
Re: Save System Between Scenes
Sounds good!
-
- Posts: 54
- Joined: Mon Jun 21, 2021 7:48 pm
Re: Save System Between Scenes
Hey Tony, pretty much got this working now, only one last hangup.
I have the Dialogue Manager as a prefab now that survives scene changes, but when the scenes change it looks like my buttons lose reference to the scripts on the Dialogue Manager.
I have the prefab in all scenes, and it has the same setup as in the DemoScenes.
I also have my Audio Manager set up the same way and that is working (not losing the reference) but my script for that is a bit different in how it is instanced.
I found in the SaveSystem.cs script that it is set up as below:
I have the Dialogue Manager as a prefab now that survives scene changes, but when the scenes change it looks like my buttons lose reference to the scripts on the Dialogue Manager.
I have the prefab in all scenes, and it has the same setup as in the DemoScenes.
I also have my Audio Manager set up the same way and that is working (not losing the reference) but my script for that is a bit different in how it is instanced.
Code: Select all
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(instance.gameObject);
instance = this;
DontDestroyOnLoad(gameObject);
}
}
Code: Select all
private void Awake()
{
if (m_instance == null)
{
m_instance = this;
if (transform.parent != null) transform.SetParent(null);
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
- Attachments
-
- Saver4.JPG (79.93 KiB) Viewed 1753 times
-
- ButtonSave.JPG (41.8 KiB) Viewed 1753 times
Re: Save System Between Scenes
Hi,
To maintain a reference to the Dialogue Manager, your button must be in the Dialogue Manager's hierarchy or set to Don't Destroy On Load in the same scene in which the Dialogue Manager came into existence.
By way of explanation, let's say you have this setup:
Scene A GameObjects:
If you play either of the scene above individually, directly in play mode, they'll work fine.
However, say you start in Scene A. Then you move to Scene B. Now these GameObjects exist:
Active Runtime Scene(s):
To maintain a reference to the Dialogue Manager, your button must be in the Dialogue Manager's hierarchy or set to Don't Destroy On Load in the same scene in which the Dialogue Manager came into existence.
By way of explanation, let's say you have this setup:
Scene A GameObjects:
- Dialogue Manager A
- Button A: points to Dialogue Manager A
- Dialogue Manager B
- Button B: points to Dialogue Manager B
If you play either of the scene above individually, directly in play mode, they'll work fine.
However, say you start in Scene A. Then you move to Scene B. Now these GameObjects exist:
Active Runtime Scene(s):
- Dialogue Manager A
- Button B: points to Dialogue Manager B
-
- Posts: 54
- Joined: Mon Jun 21, 2021 7:48 pm
Re: Save System Between Scenes
That makes sense, so I think I would basically have to make my regular canvas into a prefab and set it to Don't Destroy On Load or something along those lines. Ok, I'll give it a go. Going to do a bit more research as well.If you play either of the scene above individually, directly in play mode, they'll work fine.
However, say you start in Scene A. Then you move to Scene B. Now these GameObjects exist:
Active Runtime Scene(s):Since Button B was configured to point to Dialogue Manager B, it now has a missing reference.
- Dialogue Manager A
- Button B: points to Dialogue Manager B
Re: Save System Between Scenes
Or hook up your UI Buttons to script methods that find the current Dialogue Manager using DialogueManager.instance -- or even just use the DialogueManager script methods directly. For example:
Code: Select all
// If this script is on a UI Button, you can configure the Button's OnClick() to call StopCurrentConversation:
public void StopCurrentConversation()
{
DialogueManager.StopConversation();
}
// Or, if the Dialogue Manager has a custom script of your own that the Button needs to use, configure OnClick():
public void RunMyCustomMethod()
{
DialogueManager.instance.GetComponent<MyCustomScript>().MyCustomMethod();
}
-
- Posts: 54
- Joined: Mon Jun 21, 2021 7:48 pm
Re: Save System Between Scenes
Hey Tony, got it to work by using the script methods! Confirmed that it is saving them to the slots, I also have data from other scripts outside of the Dialogue Manager that I would like to save to the same slot. Is there a direct spot where I can store those variables as well?
-
- Posts: 54
- Joined: Mon Jun 21, 2021 7:48 pm
Re: Save System Between Scenes
Thanks Tony, just what I needed! I'm now set on the save system and it's working great.
Re: Save System Between Scenes
Awesome! Glad to help.