//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)
);
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;
}
Code Example |
---|
:: |
Csharp :: |
:: tinyint in c# |
Csharp :: make invisible unity |
Csharp :: loop over enum values |
Csharp :: displayname c# |
:: |
Csharp :: how to make a specific scene load only on game start in unity |
:: |
:: |
Csharp :: |
Csharp :: c# letters only |
:: |
Csharp :: |
:: |
:: |
:: |
:: |
Csharp :: |
:: C# Program For Check Total Occurrence Of A Number In An Array |
Csharp :: log to console c# unity |
:: const class in c sharp |
:: why v-slot not working in vue 3 |
:: |
Csharp :: c# foreach on a dictionary |
:: |
:: |
Csharp :: |
Csharp :: |
:: debug.log |