- Home /
ExecuteInEditMode doesn't work on instantiated list of objects?
I have a prefab of a single tile, and for level design purposes I want to be able to make it 'invalid' and change it's colour to red both in play and edit modes. It has a bool checkbox in the inspector which allows me to do this.
I have made sure to include [ExecuteInEditMode] at the top of this script, and the below code WORKS JUST FINE in both play mode and edit mode.
[SerializeField]
private bool isBlocked;
private Renderer rend;
void Awake(){
rend = gameObject.GetComponent<Renderer> ();
}
void Update(){
DisplayTileValidity ();
}
void DisplayTileValidity(){
if (isBlocked || isOccupied) {
rend.sharedMaterial.color = Color.red;
} else {
rend.sharedMaterial.color = Color.white;
}
}
These tiles are prefabbed, and used as a gameobject to be instantiated in a grid... The code I am using is here, and WORKS FINE except for that I now cannot change the colour of the tile using that checkbox. It won't work in edit mode, or play mode.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGenerator : MonoBehaviour {
[Header("Grid Size")]
[SerializeField]
private int rowsAmount = 3; //The width of the level
[SerializeField]
private int columnsAmount = 3; //The length of the level
[Header("Parents and Prefabs")]
[SerializeField]
private GameObject tilePrefab; //The tile prefab gameobject.
[SerializeField]
private Transform tileParent; //The Tile Parent gameobject for storing our tiles nicely in the hierarchy.
private List<Vector3> gridPositions = new List<Vector3>();
void Awake(){
GenerateGrid ();
}//end awake
public void DefineGridPositions(List<Vector3> gridPositions){
gridPositions.Clear ();
for (int x = 0; x < rowsAmount; x++) {
for(int z = 0; z < columnsAmount; z++){
Vector3 gridPosition = new Vector3 (x, 0, z);
gridPositions.Add (gridPosition);
}//end for column
}//end for row
}//end define grid positions
public void CreateGrid(List<Vector3> gridPositions){
foreach(Vector3 gridPosition in gridPositions){
GameObject tile = Instantiate (tilePrefab, gridPosition, Quaternion.identity) as GameObject;
tile.transform.parent = tileParent;
}//end foreach
}//end create grid
public void GenerateGrid(){
DefineGridPositions (gridPositions);
CreateGrid (gridPositions);
}//end generate grid
}
I assumed that by instantiating a whole list full of these tiles that I would simply be able to click whichever one I want in the hierarchy and make it red/ invalid, but that doesn't seem to be the case.
Do I have to add a script to the tile holder object or a function to the grid generator script to iterate over these tiles and change their material colour that way?
Please remember that the solution must work in both play and edit modes.
Thanks very much.
Answer by Bunny83 · Dec 10, 2017 at 09:29 PM
I'm sure it does work perfectly, however you use the sharedMaterial. So each tile uses the same material. So if you have multiple objects all with the same material and all with this script attached, only one of them (the last one that is executing) will have an effect on all objects. If you want to change material properties only for a single instance you should use .material
instead of .sharedMaterial
.
However keep in mind that this will break batching. If you only have two colors you want to toggle between you should simply create two seperate material and just exchange the sharedMaterial of the renderer you want to toggle.
Sweet! Thanks! Glad it was a simple fix.
When using a single tile in the scene, Unity actually flashes up a warning if you use 'material.color' saying:
Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.shared$$anonymous$$aterial ins$$anonymous$$d. UnityEngine.Renderer:get_material()
...which was my reason for doing it in the first place. Any idea why Unity dislikes material.color for a single object? It works fine, but Unity just complains.
But yeah. Changing it to 'material.color' throws no warnings when you generate a grid of tile prefabs, and it works perfectly in both edit and play modes :)
In the interest of keeping batching intact I am going with your 2x materials option.
Your answer
Follow this Question
Related Questions
Emissive Material control via script (code) 1 Answer
How to pass color of two colliding gameobjects to each other? 2 Answers
Material cant be seen until Im very close 1 Answer
How to check if two objects share the same material within an if statement? 0 Answers
Set Specific Child's Material 2 Answers