Activate object in another scene

I have created a keybind system. The problem is that it only works if I start in the Keybinds scene. The objects there load the keybind data and put it in a dictionary which different classes then use. When starting in the main menu or in game those objects never get activated and therefor the dictionary is empty.

public class GameController : MonoBehaviour {

    public static Dictionary<string, KeyCode> keyCodes = new Dictionary<string, KeyCode> ();
public class KeyBind : MonoBehaviour {

    public string key, defaultKey;
    public bool updated = false;

    void Start() {
        if (!GameController.keyCodes.ContainsKey(key)) {
            GameController.keyCodes.Add (key, (KeyCode)System.Enum.Parse (typeof(KeyCode), PlayerPrefs.GetString (key, defaultKey)));
            transform.GetChild (0).GetComponent<Text> ().text = GameController.keyCodes [key].ToString ();
        }
    }
}

I need to in some way load the keybind data from another scene. Thanks for your help.

EDIT: I have read that using playerprefs is a bad way to save data so I will change that

Edit 2: I could have it in the game scene but it really isn’t a preferred way as it then won’t be accessible from the main menu without “starting” the game

You could keep the object around with DontDestroyOnLoad.
Another option might be that if you’re filling this dictionary with data from a file, or something… it’s already static so you can access it from other scenes, so you could write a static function that initializes (/loads) whatever you need into it.
Then, you could call that in Awake() in a scene - just once , whenever.

The problem with your second solution is that it takes data from the keybinds in that scene. It needs the key and default key variables from there. Would the first solution work if that scene hasn’t been loaded?

Edit: the keybind values are used elsewhere so I still need to assign those variables and it would be unnecessary to write the same thing again when loading the keybinddata

Maybe I’m a little lost trying to follow… If you need the values from the scene, as you said in response to my second suggestion… From where do you get them? Could they not be in a function that’s called instead of (just) the scene? Not even sure if at this point we’re on the same page… Sorry :slight_smile: lol.
The first solution won’t really help if the scene hasn’t ever been loaded… that was just an option to keep it around later, beyond just the static variable (which may not even be needed).

The dictorynary get’s it’s values from the keybinds in the OptionsKeyBinds scene so it needs to acces the keybinds in that scene so it can get it’s data (key and default key variables). You can see this in the second attached code. Do you want something more like a picture of the scene?

Ok… I mean I kinda got that from the code.
But could you not load that data just in code (whether it’s written into the code or read from a file)?

That doesn’t mean you couldn’t alter it in that scene, redoing keybinds or whatever…
Does that make sense?

The problem is that it’s loading the data in keys that is stored in the keybinds so that I don’t both need to add it to the keybind itself and to the load code. It would be possible but it feels unnecceray to write every key and default value twice.

I do not want to get confused in how you’re explaining it and mix anything up…

Let’s say your code is working great for the scene, right?

Let’s say you save this data, yes? For re-use when restarting the game? That so?

So, use that data to repopulate your dictionary (this could be done from the static function as I mentioned).
Then, you could use the data to fill in any UI fields or whatever when the user switches to that scene, of course…

Is this kinda making sense? Or am I all wrong? :slight_smile:

The only problem is that to fill out the dictornary it needs to know which keys that are stored in the file. I could reeneter the data like

PlayerPrefs.getString("WalkUp1", (KeyCode)System.Enum.Parse (typeof(KeyCode), PlayerPrefs.GetString ("WalkUp1", "UpArrow"))

for example but it would be unefficent to write every single keybind again when the “WalkUp1” and “UpArrow” already is stored in the keybinds. If there only was a way to get the data from the keybinds in that scene so that I could load the data in another scene something like this:

PlayerPrefs.getString(keyBind.key, (KeyCode)System.Enum.Parse (typeof(KeyCode), PlayerPrefs.GetString (keyBind.key, keyBind.defaultKey)

Then I could loop it instead of adding twenty lines of code loading every value manually

I do not mean to sound like a broken record… honestly.

This is how I imagine it… I’m going to write it out long-form so you can point out any misunderstandings I might have.

You have ‘default’ keybinds. They are set once. So, these are saved… can be retrieved…

Now, you have a scene where the keybinds can be displayed and altered and of course saved & (re)-used.

If those thoughts/assumptions are correct, then you could store all of the defaults, load them up from anywhere at the start and store them…
Then, anytime they’re changed they can be updated in the list (and the updates saved, of course).

From this, is there something I’m saying that’s not right about your setup/goals?

Well I probebly could create a new list with all the defaults and keys.

Okay, I’m not entirely sure what you mean by new list (unless you mean the same list that would have been made when that scene loaded for the first time).

Besides that, would you say that how I described it is accurate for your situation?

It’s late in the day for me, and I might not be able to reply further for some time. Hopefully some of this discussion may help you in finding a solution. :slight_smile:

Yes your explanation is accurate