- Home /
Question by
michaeldorst · Jan 04, 2017 at 11:10 AM ·
materialeditor-scripting
Setting an object's material causes strange behavior in custom editor
I have an editor script which is supposed to allow me to change the texture of a tile (I'm making a game board). It works by creating a drop down menu with different "tile types", and then when I select a new tile type from the menu it will change the texture to the appropriate type.
[CustomEditor(typeof(Tile))]
public class TileEditor : Editor {
enum Type {
Blank,
Portal
}
// Represents the type of tile this is
Type type;
public override void OnInspectorGUI() {
Tile tile = (Tile)target;
// Create a dropdown list of tile types
type = (Type)EditorGUILayout.EnumPopup ("Type", type);
switch (type) {
case Type.Blank:
tile.GetComponent<Renderer> ().material = Resources.Load ("Materials/Blank Tile", typeof(Material)) as Material;
break;
case Type.Portal:
tile.GetComponent<Renderer> ().material = Resources.Load("Materials/Portal Tile", typeof(Material)) as Material;
break;
}
}
}
If I remove the lines where I set the materials, the drop down menu works as expected, but with those lines included, when I select Portal from the drop down it will change the texture for just a moment, and then both the texture and the menu will reset themselves to Blank.
What is going on here, and how do I make my selections persist?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612103945im_/https://answers.unity.com/themes/thub/images/avi.jpg)