- Home /
Question by
ApoLLoSkiLLz · Aug 31, 2016 at 05:38 AM ·
c#unity 5javascriptrandom.rangecolor change
Script sets color to multiple objects from instead of only one object with Random.range()
have made a c# script that sets a color to my objects (in this case tiles). The problem is it sets a color to multiple objects instead of only one object. I have 16 Objects (tile) that are children of one empty gameobject (tiles). Every 1.5 sec (for example) I want only one tile to change color, randomly.
As well c# as javascript solutions are welcome.
using UnityEngine;
using System.Collections;
public class TileManager : MonoBehaviour {
private float timer = 0.0f;
Color red = Color.red;
Color blue = Color.blue;
Color green = Color.green;
//Color white = Color.white; FOR LATER USE
public Transform tiles;
void Update ()
{
timer += Time.deltaTime;
if (timer >= 1.5f)//change the float value here to change how long it takes to switch.
{
Transform tile = tiles.GetChild (Random.Range (0, 16));
int rand = Random.Range (1, 4);
if (rand == 1) {
tile.GetComponent<Renderer> ().material.color = red;
} else if (rand == 2) {
tile.GetComponent<Renderer> ().material.color = blue;
} else {
tile.GetComponent<Renderer> ().material.color = green;
}
timer = 0;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Smooth Camera Background Color Changing? 1 Answer
What is Commercial Game? 1 Answer
GetComponent, What is it ? 1 Answer
Reset Score due to pressing button? 2 Answers
Code to randomly generate a mesh? 0 Answers