Create GameObjects in Parent based on Index Count of Files

Hi everyone, I am in need of some help in which I am sure I am making a rookie mistake.
I have a save manager for my game which creates files in the saves folder. I am able to list these files, however, I would like to create an array of gameobjects based on the count of saved files.

My UI has a save slot as a gameobject, and it is that which I wish to clone in the SavesHolder (parent) based on the count. However, the Instantiate does not work, which again I am sure is based on my lack of understanding.

There is a checksavedgames function that is called in the Awake method.


Variables:

[Header("Saved Game Slots")]
[SerializeField] private GameObject slotHolder;
[SerializeField] private GameObject gameSlot;
[SerializeField] private string[] savedFiles;
[SerializeField] private GameObject[] slots;

[Header("Save Game Checks")]
[SerializeField] private Boolean firstSave = false;
[SerializeField] private int savedGamesIndex;

And here is the method:

private void CheckSavedGames()
    {
        // Get the list of saved files
        savedFiles = Directory.GetFiles("saves/");
        savedGamesIndex = savedFiles.Length;

        // Create slots according to the saved files}
        for (int i = 0; i < savedGamesIndex; i++)
        {
            slots[i] = Instantiate(gameSlot);
        }
    }

Any help would greatly be appreciated.

If you don’t understand it, we understand it even less! Here is how you can change that right away:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Since you are dabbling specifically in loading/saving, here are my own notes on this process:

Load/Save steps:

https://discussions.unity.com/t/799896/4

Don’t use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

1 Like

Wow thanks for the above info, this is very helpful. After combing google for 2 weeks this makes more sense than the rubbish I found online which just confused me more. Thank you for your time and effort to help me:)

1 Like