Search Unity

Prevent initialization of public class? (~don't touch anything!)

Discussion in 'Scripting' started by by0log1c, May 19, 2011.

  1. by0log1c

    by0log1c

    Joined:
    Jan 30, 2011
    Posts:
    40
    Hey guys, I'm trying my luck on these forums once more :)

    So I've got a JS class used to store various data about the parent GameObject. I've made an Editor script to fill it up with everything I need. All is well except starting the game causes my script to re-initialize, therefore losing all stored data.

    Is there any way to prevent this? I need to fill my Array() and List.<> in EditorMode and the values to be kept ad vitam aeternam.

    Maybe I should have my class as a variable inside a container script component rather than dragging the class on the GameObject itself?

    I might be far off but I've faced this problem before and its a serious hold-back.
     
  2. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    Regular Objects don't persist from the editor for the game as far as I know. I have dealt with this issue before. A way around it in your case could be to use a Component and populate it's public fields with your editor script. They should persist to the game state. Certain things which aren't serializable (ie: don't show up in the inspector window for that component) probably wont transfer over to the game state. You can make your custom classes serializable by adding the System.Serializable attribute to them.
     
  3. by0log1c

    by0log1c

    Joined:
    Jan 30, 2011
    Posts:
    40
    Thanks for the answer. I managed to fix one of my class by making my variables non-private (in a public class). I simply hide them for the Inspector and those builtin were persistent again, as intended. :D

    Now the core of my system was an ArrayList holding objects(class instances) of various type to be used later. The only types that I can get to persist through Unity state changes are int,float,String,boolean and built-in arrays.

    My concern are keeping the data as safely as possible AND keeping the script editable without losing anything. I can't seem to achieve that.

    I'm thinking I might have to prepare arrays of every type I foresee. Add the user objects inside the correct array then keep a reference as to which comes after which, but I'd prefer to avoid such a mess.

    Any hints are appreciated, sorry for the poor question but I couldn't form it any better :s
     
  4. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    I realize this response as late as I have been really busy recently. Probably you have already solved your issue but I'll give you my thoughts anyways.

    I have managed to avoid using ArrayLists entirely. If your objects all derive from a base-type (and if they are Monobehaviours then they do) then it's easy to just make an array Monobehaviour[].

    Since I don't know what exactly you are putting in your ArrayList I can't really say how to optimize it, but my advice would be to avoid that structure altogether.