Search Unity

A little help here?

Discussion in 'Scripting' started by ifrit, Aug 29, 2012.

  1. ifrit

    ifrit

    Joined:
    Mar 20, 2012
    Posts:
    7
    Hello gentlemen! :)
    Suppose I have a simple static array in a script, and I initialize this array using Awake or Start!
    Now, suppose that I have other scripts that changes this array in edit mode.
    The problem is, when I enter play mode the Awake or Start function is called again resetting my array.

    Does anyone know how to keep the changes after I enter play mode?
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Don't initialize it in Awake or Start.
     
  3. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Oh, static. Whoops. Static variables don't save their values between editor and game mode (that I"ve found). I think it's because Unity saves variables per-object, not per-class.

    I use 'singleton objects' instead to get around that:

    Code (csharp):
    1. class Thing extends MonoBehaviour {
    2.   var instance : Thing;
    3.   function Start() {  instance = this; }
    4.   var pretendStaticArray : int[];
    5.  
    6.   static function GetStaticArray() : int[] { return instance.pretendStaticArray; }
    7. }
     
  4. ifrit

    ifrit

    Joined:
    Mar 20, 2012
    Posts:
    7
    Thanks, I'll try it out!