- Home /
Making random tiles
Hey guys,
simple question with probably a simple answer here, but still difficult enough for me to not find anything about how to solve it... I want to make a grid of tiles in random colors, picked out of 7 preset colors. So I made a script for it, attached it to the tile gameobject, but... the field of tiles always has the same random color (like every tile blue, or red), not every tile a different color as I want it to... How do I solve this? I'm sure there must be some easy solution for it, not having to make a different script and different material for every tile? So how do I make the script run again for every tile prefab in the field and not just once for the main tile? I'm working with C#.
Answer by Piflik · May 07, 2012 at 03:57 PM
Don't know how your script looks like (hint), but my first try would be something like this (JS):
function Start() {
var ranNmbr = Random.Range(0, 6);
switch(ranNmbr) {
case 0 : transform.renderer.material.color = Color.red;
break;
case 1 : transform.renderer.material.color = Color.blue;
break;
case 2 : transform.renderer.material.color = Color.green;
break;
case 3 : transform.renderer.material.color = Color.yellow;
break;
case 4 : transform.renderer.material.color = Color.cyan;
break;
case 5 : transform.renderer.material.color = Color.magenta;
break;
}
}
Hey man, thanks for answering so soon :)
$$anonymous$$y code is pretty much similar to yours (I think, never worked with java):
using UnityEngine; using System.Collections;
public class ColorRandom : $$anonymous$$onoBehaviour {
public Color kleur;
public $$anonymous$$aterial material;
int c;
// Use this for initialization
void Start () {
c = Random.Range(0, 7);
if (c == 0)
kleur = Color.black;
if (c == 1)
kleur = Color.white;
if (c == 2)
kleur = Color.red;
if (c == 3)
kleur = Color.green;
if (c == 4)
kleur = Color.yellow;
if (c == 5)
kleur = Color.magenta;
if (c == 6)
kleur = Color.cyan;
if (c == 7)
kleur = Color.blue;
material.color = kleur;
}
}
Did test yours on another tile prefab, and the funny thing is that that tile also gives the same color as the others (I guess because it still is the same material attached to it)... Now to see if I can do anything with that information, though having to make like 25 different materials for each tile... dunno, there must be an easier way to do that...
I just tested my script on a cube prefab and it works. Don't know anything about C#, so I can't say what's wrong with yours.
When you edit a material on runtime, it is made a unique clone and shouldn't affect the material of other objects.
Edit: I know whats wrong...you give the script a material via the inspector, and change it, so it is changed globally (the material itself). You have to use gameObject.renderer.material.color to edit the material assigned to the prefab directly.
it seems that you have one script on a tile prefab. both your scripts will only work when individually attached to each tile that is to change colour. To change the tile colour of each tile that is a child of the prefab tile collection with just one script, you'll have to step through each child and apply the random colour to each tile separately.
i.e. i think @Laar has a group of tiles in a prefab , and wants to change the tiles colour with just One script on the parent prefab.