Updated on August 3, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on July 31, 2018

Hello guys , im new member and i need your help.I saw the video on youtube of brackeys that makes a switch colour.He use sprite renderer in script to make his ball takes the same colour like random colour, so i need your help to tell me how i can make the same thing in 3D game( i want my ball take the same colour like the random colour script). 

Thanks in advance

  • Liked by
0 on August 3, 2018

I’d start with a basic understanding here:

And this for a more detailed coding description with examples:

so once you’ve created a color, and are ready to make a new random color, you could create a new random color by creating a new color with random parameters. I would probably even probably create a custom method to help you do that, but it wouldn’t really be needed…

// the custom function to get a random number between 0 and 255 
int RandColorNum(){
  return Random.Range(0,256); returns a number 0 - 255 but not 256
// put this in the code section you need to
float alpha = 1.0f; // how transparent you want the color, 0 - 1, 0 being clear
Color randomColor = new Color(RandColorNum(), RandColorNum(), RandColorNum(), alpha);  //create the random color
yourGameObject.GetComponent<Renderer>().color = randomColor; // apply new color to the material of your game object

note: I didn’t test this out, but it should be good…

  • Liked by