Created
August 29, 2022 06:39
-
-
Save PrashantUnity/5c8799acc3c0aa3bd6601e8bd33e8e56 to your computer and use it in GitHub Desktop.
Setting Random Material color to game object inside unity editor
This file contains hidden or 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