Search Unity

Activate object in another scene

Discussion in 'Getting Started' started by MCrafterzz, Jun 15, 2017.

  1. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    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.

    Code (CSharp):
    1. public class GameController : MonoBehaviour {
    2.  
    3.     public static Dictionary<string, KeyCode> keyCodes = new Dictionary<string, KeyCode> ();
    Code (CSharp):
    1. public class KeyBind : MonoBehaviour {
    2.  
    3.     public string key, defaultKey;
    4.     public bool updated = false;
    5.  
    6.     void Start() {
    7.         if (!GameController.keyCodes.ContainsKey(key)) {
    8.             GameController.keyCodes.Add (key, (KeyCode)System.Enum.Parse (typeof(KeyCode), PlayerPrefs.GetString (key, defaultKey)));
    9.             transform.GetChild (0).GetComponent<Text> ().text = GameController.keyCodes [key].ToString ();
    10.         }
    11.     }
    12. }
    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
     
    Last edited: Jun 15, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  3. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    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
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :) 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).
     
  5. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    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?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  7. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    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.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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? :)
     
  9. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    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
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. 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
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  11. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Well I probebly could create a new list with all the defaults and keys.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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. :)
     
  14. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Yes your explanation is accurate