Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Many Questions : Classes and Scene

Discussion in 'Scripting' started by Kthaesler, May 8, 2015.

  1. Kthaesler

    Kthaesler

    Joined:
    Jan 5, 2015
    Posts:
    4
    I am trying to develop the structure for a game that I can back fill with content later and am getting stuck on how to program certain portions of the game.
    This is what I would like to make;
    1st scene, empty slots to hold characters (select existing character : scene 4, select empty slot : scene 2)
    2nd scene, select race
    3rd scene, select class
    4th scene, Menu - for now just "play game"
    5th scene, actual game

    Currently I understand I should have a CHARACTER class that has all my variables I need set as null. This way when the user clicks on "HUMAN" currentCharacter.race can be set equal to baseHuman.race. Same applies to class: currentCharacter.class = baseFighter.class
    Is this a good way to do things, or am I going about it all wrong?

    How do I save the information that is generated between scenes. Right now if I set my character race in scene 2, when I set my class in scene 3 the character race is null.
    How about saving the character after playing? Do I just export it to a file? When I export it do I have to export each variable individually and import them individually or is there an easier way that if I load a file it will automatically load the right numbers into the right variables.
    Right now it isn't a big concern since I have only a dozen variables, but once implementing an inventory and each item in the inventory has its separate variables I would concerned with keeping everything in order.
     
  2. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    To keep informations between different scene, 2 solutions:
    either you use the PlayerPrefs, or you can have a script which will contain all your variable you want to keep : you put this script on an empty object and in this script you call the "DontDestroyOnLoad" fonction. It will make you keep your object (with the script attached) even when you change your scene.
     
  3. arty155

    arty155

    Joined:
    Apr 17, 2015
    Posts:
    22
    I have a similar setup, where I use DontDestroyOnLoad.
    http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

    When choosing a character, you will keep a reference to that character between scenes. So from character creation you can go from Scene2 to Scene3 and your character choice is kept. Then when in Scene3 you could just add a script to your character game object, that adds your "class" script. I do the same thing, but I do it at runtime in game, where the player can choose their class and it adds that "class" script to the player game object. Then all skills of that class become available to the player.

    There are a couple of ways to handle saving the data, the easiest is using PlayerPrefs. The other way is to pass all the data to a database. There are a few free assets in the Unity store that help with passing more than strings out to PlayerPrefs.