Search Unity

Get datapath during serilization

Discussion in 'Scripting' started by rainboww, Apr 7, 2019.

  1. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    I declare a bunch of variable in my path class.

    Code (CSharp):
    1.      
    2.  
    3. public static class p {
    4.         static public string gamepath= Path.GetDirectoryName(UnityEngine.Application.dataPath);
    5.         static public string localpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), g.GameName);
    6.         readonly static public string Resourcesfull = p.Combinem(gamepath, "Assets", "Resources\\");
    7.  
    8.         static public string gamesetpath = p.Combine(gamepath, "gameset\\");
    9.  
    There are many more variables that base on gamepath.
    Today i suddenly got:

    Code (CSharp):
    1. UnityException: get_dataPath is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'execute' on game object 'script'.
    I did get it sometimes before but the construction worked in general.

    Now i have three questions:
    Why does it sometimes work and sometimes not?
    Why does unity3d insist on it not be called during serialization?
    What can i do now in no case i want to rewrite that variables so i need the data path during serialization?
     
    Last edited: Apr 7, 2019
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I guess, as long as serialization goes on, it won't work. For your example, the run cycle is not always equal to the previous, so maybe it sometimes is done serializing, sometimes it is not. So you better wait for that serialization to happen before going on to the next part? just a general guess as your example does not really tell, where it happens.
     
  3. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    I added where it is to my question.
    It is directly declared in the class (not in the awake or start function like unity seems to insist unity insists it must only be declared in the mainthread).