0
\$\begingroup\$

Here is my noobish scenario I'm trying to understand:

Some class, Terrain:

       void Start()
        {
...
            switch (Type)
            {
                case TerrainType.StartPoint:
                    renderer.material.color = Color.blue;
                    break;
                case TerrainType.EndPoint:
                    renderer.material.color = Color.red;
                    break;
                case TerrainType.Wall:
                    renderer.material.color = Color.black;
                    break;
                default:
                    renderer.material.color = Color.green;
                    break;
            }
...
        }

some other class, SomeOtherClass: ('target' is Terrain object)

void Awake()
{
...
temp = (Terrain) Instantiate(target, new Vector3(0,0,0), Quaternion.identity);
...
}

At this point everything is ok.

If, down the road, i try assigning different type to one of the existing Terrain objects, the type will change sure, but the color will not since color change switch is in Start()

If i put the color updating code in Update() function sure it works and the color is changed but is this the right way of doing something like this?

I feel like it is an overkill since Update is called every frame and i might have only one or two calls to change the type throughout the whole game life time.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Trigger an event, or just call the change method when you change the type of terrain object. Pull you color switching code out of start and put it in its own method. Then, have start call the SetColor method, and have your code that switches terrain types also call SetColor whenever the type changes.

It's not something that needs to be done every frame, you're correct there. It's something that needs to happen when something else happens. So, you just call the code when that other event happens (in this case changing the terrain type).

\$\endgroup\$
2
  • \$\begingroup\$ it sure is nice not having to make that setColor call explicitly when the code is in Update() though :) \$\endgroup\$
    – Dust
    Commented Oct 19, 2014 at 20:46
  • \$\begingroup\$ Sure, that's an option. It's just not good practice. \$\endgroup\$
    – House
    Commented Oct 19, 2014 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .