Change material.color in code and revert on Quit

Hi everyone,

I’m using something like this to change the color of several objects.

skinMat.color = color;

skinMat is a Material attached in the inspector. The problem is when I change the color in-game, it keeps it afterwards. I’ve tried a bunch of things and no matter what it always keeps the color I chose in-game. I’d like to have the material revert back to its original state when I stop the game in the editor. There MUST be a way to do this, in code or otherwise.

Thanks in advance,

Jason

You’re better off instantiating the material.

var mat : Material;
private var matInstance : Material;

function Start () {
	matInstance = Instantiate(mat);
	renderer.material = matInstance;
	matInstance.color = Color.blue;
}

That way the original material is never touched, so if something ever goes wrong and OnApplicationQuit is never called (e.g. you accidentally write an infinite loop and have to force-quit, a crash, whatever), it’s not a problem.

You could save it off on Start and restore it in OnApplicationQuit http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnApplicationQuit.html