- Home /
ExecuteInEditMode changes lost on play
I've got a simple script that lets me set multiple materials at once in edit mode via a simple bool called "toggle." In this instance, the bool is tied to whether or not a wall can be jumped off of, which the player needs to see a visible difference to know which walls to utilize. [Some of the following code is irrelevant, I'm just talking about the "Toggle" type here.] using System.Collections; using System.Collections.Generic; using UnityEngine;
[ExecuteInEditMode]
public class RendererSetup : MonoBehaviour {
enum Types {
AllTheSame,
Toggle,
PerRenderer
}
[SerializeField] Types type;
public Renderer[] renderers;
public Material[] materials;
public bool toggle;
public Material materialOff;
public Material materialOn;
bool materialsSet;
void Start () {
if (Application.isPlaying) {
SetMaterials();
}
}
void Update () {
if (Application.isPlaying && !materialsSet) {
materialsSet = true;
SetMaterials();
}
if (!Application.isPlaying) {
SetMaterials();
if (materialsSet)
materialsSet = false;
}
}
void SetMaterials() {
if (type == Types.AllTheSame) {
foreach (Renderer r in renderers) {
r.material = materials[0];
}
}
else if (type == Types.Toggle) {
foreach (Renderer r in renderers) {
if (toggle) {
r.material = materialOn;
}
else {
r.material = materialOff;
}
}
}
else if (type == Types.PerRenderer) {
for (int i = 0; i < renderers.Length; i++) {
renderers[i].material = materials[i];
}
}
}
}
This works fine when something is updated in the editor, and typically changing the "toggle" bool changes the materials accordingly. When I hit play, the material changes are lost, even though my variables remain the way they were... I've confirmed that the Start function is called on hitting play... so why aren't my materials updated?