Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

unity change material

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 by gist.github.com #
 
PREVIOUS NEXT
Tagged: #unity #change #material
ADD COMMENT
Topic
Name
9+4 =