Making a main menu with the saving system

Hi guys,
I’m trying to make a main menu with a loadlastscene option,
I got a new method for the saving wrapper which is:
public void LoadFromMenu()
{
LoadLastScene();
}
which is:
public IEnumerator LoadLastScene()
{
yield return GetComponent().LoadLastScene(defultSaveFile);
Fader fader = FindObjectOfType();
fader.FadeOutImmediate();
yield return fader.FadeIn(fadeInTime);
}

(the 1’s are on purpose haha)
and the UI button has an on click for this:
public void OnClickMouse()
{
GetComponent().LoadFromMenu();
}
even though the simplicity, it doesn’t work…
tells me a "NullReferenceException: Object reference not set to an instance of an object
Load.OnClickMouse () "
I guess the problem is the save file is null (the file does exist of course), and I couldn’t find how to change it…
broke my head about it quite a lot but couldn’t find an answer… please help:)
thanks in advanced!

Hi,
I got it to load the right scene with:
StartCoroutine(LoadLastScene(“save”));
from the savingsystem.cs
but it doesn’t load the vector3 etc… tried putting a Load(“save”) after, but it doesn’t work either…
if I press the load() key after the scene has loaded (through the savingwrapper) it works,
but when tried to getcomponent and load right after the LoadLastScene() I get a null reference again…

Hi,

In which course and lecture are you? And what do you mean by “it doesn’t load the vector3 etc”? What is “it”?


See also:

I’m in the RPG inventory course, a few lectures from finishing it
“It” is the game/load method… as I wrote, I called the Load method from the savingsystem and savingwrapper (tried both) though it did not load the saveable parameters that are supposed to restore from this method
sorry if Im not clear enough, let me know if anything else is not clear

Don’t worry. I was just confused because I’m a teaching assistent in the Unity 2D and 3D courses, and I do not know the content of the rpg course. I’ve just added the correct tag, so it is easier for @Brian_Trotter to find your thread. :slight_smile:

thanks! :slight_smile:
he is really helpful… :wink:

Hi shakedhe,
Just for the sake of clarity, I want to make sure your GetComponents are configured properly.

public IEnumerator LoadLastScene()
{
yield return GetComponent<SavingSystem>().LoadLastScene(defultSaveFile);
Fader fader = FindObjectOfType<Fader>();
fader.FadeOutImmediate();
yield return fader.FadeIn(fadeInTime);
}

GetComponent() and FindObjectOfType() require the types to be specficied with a declaration before the parenthesis, so that these functions know what you’re looking for. I’m sure you actually have these things set (or it won’t compile).

It’s likely that the script holding OnClickMouse() isn’t on the same GameObject as the SavingWrapper (because the SavingWrapper and SavingSystem should be loaded dynamically in the PersistentObject prefab). GetComponent can only find components attached to the same GameObject as the script, so in that script, what we’ll need to use is the FindObjectOfType…

public void OnClickMouse()
{
      FindObjectOfType<SavingWrapper>.LoadFromMenu();
}

Let me know if this fixes things up for you.

yes!! it worked! thank you Brian!
so I’ve added the core prefab to the scene for the presistentobjects, to be sure its the only saving scripts in all the scenes
changed the onclickmouse to
FindObjectOfType<SavingSystem1|>().StartLoadLastScene();
(without the | of course; just for it to not disappear again)
and in the savingsystem its:
public void StartLoadLastScene()
{
StartCoroutine(LoadLastScene(“save”));
FindObjectOfType<SavingWrapper1|>().Load();
}
I’ve tried so many complicated methods… simplest always works best.
thank you, O great Brian!! :wink: (monty python not-so-obvious reference)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms