Setting Random Material color to game object inside unity editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
// | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment