Static variables loose their value after updating scripts

Hi there!

I noticed that if I modify a script while game is playing and then go back to Unity, static variables loose their values.

Here is a simple script that shows this behavior:

using UnityEngine;
using System.Collections;

public class StaticTest : MonoBehaviour {
	
	public static string s;
	
	// Use this for initialization
	void Awake () {
		s = "A";
		Invoke("SetString",1.0f);
	}
	
	void SetString(){
		StaticTest.s = "B";
		print("String set to: " + s);
	}
	
	void OnEnable(){
		print("ena, string: " + s);
	}
	
	void OnDisable(){
		print("dis, string: " + s);
	}
}

To try it just run it, after a second go to MonoDevelop, edit the script by just adding a space, save it and go back to Unity.

You can see in the console that string “s” looses its value.

Do I’m doing something wrong?

Thank you very much :slight_smile:

Anyone has an idea?

Hi there!

Basically you’re not supposed to change scripts while the game is running.

Hi Myx!

thanks for your reply!

Yes, I see! …but it was handy :slight_smile: …and since now has always worked… ok, nevermind… just stop-and-play after script changes.