So I’m currently trying to generate a world using a prefab with only a MeshRenderer on it. My goal is to generate a world and having each instances having a different color. I heard that you need to create a new material every time when u do so but it is not working right now, Any idea why ?
public class HexGeneration : MonoBehaviour {
public GameObject platformMesh;
public int height = 5;
public int lenght = 5;
// Use this for initialization
void Start () {
for (int i = 0; i < height; i++)
{
for (int j = 0; j < lenght; j++)
{
GameObject generatedPlatform = Instantiate(platformMesh, new Vector3(0,0,0), Quaternion.identity) as GameObject;
Transform platformTransform = generatedPlatform.GetComponent<Transform>();
ChangePosition(platformTransform, i, j);
MeshRenderer platformRenderer = generatedPlatform.GetComponent<MeshRenderer>();
ChangeMaterial(platformRenderer);
}
}
}
// Update is called once per frame
void Update () {
}
void ChangeMaterial(MeshRenderer platformRenderer)
{
Material material = new Material(Shader.Find("Standard"));
Color color = new Color(Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), 1);
material.color = color;
platformRenderer.material = material;
}
void ChangePosition(Transform platformTransform, float height, float lenght)
{
if (height % 2 == 0)
platformTransform.Translate(lenght * 8.66f, 0, -height * 7.5f);
else
{
if (height == 1)
platformTransform.Translate((lenght * 8.66f) + 4.33f, 0, -7.5f);
else
platformTransform.Translate((lenght * 8.66f) + 4.33f, 0, -height * 7.5f);
}
}
}
The enemies are still there. I can't destroy them.
– samosunaz