Coloring the cubes in Editor (and why it's a bad idea...)

Ok, so it’s actually simple enough to color the cubes in the EditCube script…
First make sure your start and end waypoints are exposed in your WorldMap script (I called it WorldMap, but I think Ben called it something different).
In your EditCube script, Find your waypointmap script (FindObjectOfType). Then simply compare… if the GridPos of this cube matches the GridPos of the Start, then waypoint.SetColor(Color.blue), if it compares to the end, then waypoint.SetColor(Color.red).

That being said, it’s a bad idea. Whenever you make a change to an element in a material, a material instance is created automatically. When you’re playing, this is ideal behavior, and at the end of the game, everything reverts back to where it was set… however… if you change it in the Editor script, it creates a material instance on the waypoint in the heirarchy. This is a permanent change. If you change the start waypoint in the WorldMap script, your old start waypoint will still be colored blue. On top of that, you risk a memory leak in the editor (and the editor will actually throw a warning about this if you do). Of course, the editor will recommend that you change sharedMaterial… but if you do that, ALL the cubes will turn blue…

Here’s the warning:

Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.
UnityEngine.Renderer:get_material()
Waypoint:SetColor(Color) (at Assets/Scripts/Waypoint.cs:47)
EditorSnap:ColorStartAndEndPoints() (at Assets/Scripts/EditorSnap.cs:54)
EditorSnap:Update() (at Assets/Scripts/EditorSnap.cs:28)

I went with a different solution… I flagged it instead to simply change the text to Start and End. It still changes color on run (in the WorldMap script), but visually you should be able to see Start and End just fine in the inspector (and your brain should infer the map coordinates from the surrounding tiles…).

private void ColorStartAndEndPoints()
{
    WorldMap world = FindObjectOfType<WorldMap>();
    if (world)
    {
        if (world.StartPosition.gridPosition == waypoint.gridPosition)
        {
            textMesh.text = "Start";
        }
        if (world.EndPosition.gridPosition == waypoint.gridPosition)
        {
            textMesh.text = "End";
        }
    }
}

3 Likes

Privacy & Terms