Search Unity

[SOLVED] C# Keeping static variables set in/from Unity editor

Discussion in 'Scripting' started by MetaCron, Sep 19, 2014.

  1. MetaCron

    MetaCron

    Joined:
    Sep 17, 2014
    Posts:
    2
    Hello there thanks for reading,

    so yeah this is probably quite a beginner question since I only started using Unity very recently. I've worked with the c sharp scripting language before, so I'm not completely new to scripting.
    Well now I've got a script set up on an empty gameobject in my scene in which I can change both several numeric values as strings for a custom editor I made that allows me to place 2d sprites in a tile-like manner. Things like: a String that holds a variable that defines the path where Unity would be able to fined prefab tiles for me to place them, several numeric values for like brush size and layerdepth... I can change these in the inspector of said gameobject and it manages to keep those while I'm editing my scene. But as soon as I try out my game by pressing the play game button I lose my static variable values...

    I have the following in my script which is attached to my gameobject:

    LevelLogic.tilePath = EditorGUILayout.TextField("Tile Path", LevelLogic.tilePath);

    Where LevelLogic is a class with several other static variables I'd like to keep at all times. "tilePath" is a static variable in said class.

    As I said it's possible I'm just stupid and forgot something important, being a newbie at Unity as I am. I believe I can grasp why this is happening, but I don't know how to look up this problem, sorry.

    Extra:
    -) I tried changing the LevelLogic class in a static one
    -) the game object itself static in the Unity Editor
     
    ThatCodingKid likes this.
  2. MetaCron

    MetaCron

    Joined:
    Sep 17, 2014
    Posts:
    2
    OK I think Ifound a similar thread :
    http://answers.unity3d.com/questions/437091/static-class-not-keeping-changes-after-playmode.html
    So editorprefs doesn't give me the functionality I`m looking for...I guess the best way to do this is to write all my "static" variables and objects to a file and load it back from it once I need them again. Any alternatives?
    Also I haven`t been able to look into the API yet but are there functions that are called when gamemode starts and ends, but I'll search through the API right away. Thanks.

    EDIT: Looks like I would be able to do this with a script with:
    [Executineditmode]
    The Start() function, gets called when both game AND editor starts in Unity
    And last but not least with the bool Application.isPlaying

    EDIT: 20/09 I pretty much solved this inconvenience by having my variables saved to a unique per scene file before I enter game mode and having it load again when I enter edit mode... So yeah I consider this solved :)
     
    Last edited: Sep 20, 2014
    ThatCodingKid likes this.