Question : unity how get random color to material
Answered by : wrong-weasel-j0nzocgx2g01
//using Color32
Color32 randomColor = new Color32( System.Convert.ToByte(Random.Range(0, 255)), //Red System.Convert.ToByte(Random.Range(0, 255)), //Green System.Convert.ToByte(Random.Range(0, 255)), //Blue System.Convert.ToByte(255), //Alpha (transparency)
);
Source : | Last Update : Mon, 10 Jan 22
Question : Setting Random Material color to game object inside unity editor
Answered by : prashant-priyadarshi
var newObstacle = Instantiate(cube, new Vector3(i, 0, j), Quaternion.identity, transform);
Renderer obstacleRenderer = newObstacle.GetComponent<Renderer>();
Material mat = new Material(obstacleRenderer.sharedMaterial);
mat.color = GetRandomColor();
obstacleRenderer.sharedMaterial = mat;
// for reference methods
public Color GetRandomColor()
{ return new Color(Random.Range(0, 255),Random.Range(0, 255),Random.Range(0, 255));
}
// for specific
public Color GetRandomColor()
{ var random = Random.Range(1, 10); if(random<4) return Color.red; else if(random<7) return Color.blue; else return Color.green;
}
Source : https://gist.github.com/PrashantUnity/5c8799acc3c0aa3bd6601e8bd33e8e56 | Last Update : Mon, 05 Sep 22