0

So what I am trying to accomplish is when I load a scene I want to have certain objects to be active or inactive depending on their the information saved in my files.

Example being :

In my game, when I load one of my scenes there is a wall, then I push a bookcase to a certain point and triggers the wall to be set to inactive. I exit this scene and come back to the scene and the wall is active again. What I want is when the wall is set inactive, it stays inactive even when I leave the scene and and won't appear when I come back to the scene later.

The trouble I am having is how to do this. At the moment what I have is when the player clicks the "New Game" button I generate a new Game_Info and select the Default() :

[Serializable]
public class Game_Info {

    public bool WoodWallEnabled;

    public void Default(){
        WoodWallEnabled = true;
    }
}

My WoodWall script :

public class Get_WoodWall_Info : MonoBehaviour {

    void Awake () {
        gameObject.SetActive (Helper_Manager.instance.gameInfo.WoodWallEnabled);
    }
}

I have a feeling I am not doing this the right way and that there is a better way to handle this, especially since doing like this would need a unique script for each gameobject that I would like to manipulate through my saved file (Get_WoodWall_Info, Get_WoodWall_Info1, etc...) but even if I created enourmous amounts of scripts to do this, I can't find a way to save the state of the gameobjects.

How would you handle and pass around this kind of data?

DRKblade
  • 347
  • 2
  • 13
JoeyL
  • 1,295
  • 7
  • 28
  • 50
  • I edited my answer for the other part of your question. I'm sorry for not noticing that part before. Anyway, hope that helps. – DRKblade Apr 14 '16 at 15:43

2 Answers2

1

If you are saving only WoodWallEnabled variable then simple use PlayerPref as mentioned above. Now, if you are saving the whole scene info such as object position or every variable in Game_Info class, you have to convert the class to Json with JsonUtility.ToJson, then save it using PlayerPref. Then to load it, you use PlayerPref to load it then Serialize it back to your Game_Info class with JsonUtility.FromJson.

A full working examples that includes arrays:

Serialize and Deserialize Json and Json Array in Unity

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Yup, I am going to be saving important information in the scene but not everything though. Like when I mention moving the bookcase, I have about 5 bookcases in that scene but I only care about the position of 1 of them as an example. Some scenes have very little that needs saving while some have more. – JoeyL Apr 14 '16 at 10:35
  • The solution in the link I posted above should work for you. – Programmer Apr 14 '16 at 10:41
  • Thank you Programmer I will check it out. By the way how is the security of using this method? I remember watching a Unity tutorial saying that using PlayerPrefs isn't safe as the user can just go in and edit the file to what they want. – JoeyL Apr 14 '16 at 10:44
  • Your post did not mention anything about security. You should **NOT** worry what people do in your game locally. The only time you should worry is when two players are playing online then you should implement some cheating preventing mechanism. Online gaming has nothing to do with local scores so dont worry about that. – Programmer Apr 14 '16 at 10:49
  • Now, if you still want to spend your time to prevent players from changing their scores then learn about encryption. You can encrypt it before saving it with `PlayerPref`. There are many ways to encrypt a string but I am sure you can find many when you google C# string encryption. MD5 and more of them. If this solution in the link work for you, which should, then you can accept this as answer. – Programmer Apr 14 '16 at 10:50
  • Ok good to know, yeah it is a single player game and you do make a good point that I should not worry about it locally. Thanks again. – JoeyL Apr 14 '16 at 10:54
  • Yup. Spend that time to make your game look more beautiful,fix more bugs or add new features. Let me know if that solution work for you. – Programmer Apr 14 '16 at 10:57
0

I think PlayerPref is a good choice in this situation. You can do like this:

  • Call PlayerPrefs.SetInt("wall enabled", active ? -1 : 0); to save the state of your wood wall with active is the state of the wall.

  • And in Get_WoodWall_Info, instead of using the value in the Help Manager, use PlayerPrefs.GetInt("wall enabled") != 0

Values in PlayerPrefs will be saved through game sessions, so you may want to set the wall enabled key to 0 when you start a new game.

EDIT

@JoeyL Why don't you create one script for all your game objects, like this:

public class StateHandler : MonoBehaviour {
    void Awake () {
        string keyName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + gameObject.name;
        if (PlayerPrefs.GetInt(keyName) == 0)
            gameObject.SetActive(false);
    }
    public void SetActiveState (bool state) {
        string keyName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + gameObject.name;
        PlayerPrefs.SetInt(keyName, state ? -1 : 0);
        gameObject.SetActive(state);
    }
}

This will use your current scene name and your attached game object name as the key, so you just need to make sure all gameobjects using this script have distinct names.

DRKblade
  • 347
  • 2
  • 13
  • Should the first parameter of PlayerPrefs.SetInt() in your SetActiveState(bool stat) be keyName? – JoeyL Apr 15 '16 at 10:02
  • I really do think this is what I am looking for! I am making a 2D RPG starter kit and I can work with this to make this a tool for devs as PlayerPrefs would be fine to use since what I am making isnt for online. Making a script and testing as we speak. – JoeyL Apr 15 '16 at 10:04
  • @JoeyL Oh yes, you're right, I was mistaken. And also, good luck in your game :) – DRKblade Apr 15 '16 at 11:04
  • Thank you very much for taking the time to help. You and Programmer are very awesome and patient people. Thanks again. – JoeyL Apr 15 '16 at 11:20