Search Unity

Color Generator for randomly generated coloring

Discussion in 'Scripting' started by eeveelution8, Aug 1, 2014.

  1. eeveelution8

    eeveelution8

    Joined:
    Feb 16, 2013
    Posts:
    8
    Script is for everyone, there are two parts.

    the code itself was intended for coloring different zombie clothing to add variety to the bots, but can be reused for anything

    demonstration of how it would work, is create an empty object, and put this script in it, (it's called 'colorgenerator')

    Code (JavaScript):
    1. var ID_1_shirt : Color[];
    2. var ID_2_pants : Color[];
    3. var ID_3_shoes : Color[];
    4.  
    5. var thecolor1 : Color;
    6. var thecolor2 : Color;
    7. var thecolor3 : Color;
    8.  
    9. function Start (){
    10.  
    11.  
    12.     var index1 : int = Random.Range(1,ID_1_shirt.length);
    13.     thecolor1 = ID_1_shirt[index1];
    14.  
    15.      var index2 : int = Random.Range(1,ID_2_pants.length);
    16.     thecolor2 = ID_2_pants[index2];
    17.  
    18.     var index3 : int = Random.Range(1,ID_3_shoes.length);
    19.     thecolor3 = ID_3_shoes[index3];
    20.  
    21.  
    22. }
    23.  
    and place this second script in every object you intend to have colored. (its called 'colorchoice')

    Code (JavaScript):
    1. var Colorholder : colorgenerator;
    2.  
    3.  
    4. var clothe_ID : int = 0;
    5.  
    6. function Start () {
    7. choosemycolor();
    8. }
    9.  
    10. function choosemycolor(){
    11. yield WaitForSeconds(0.01);
    12.  
    13. if(clothe_ID == 1){
    14.  
    15. renderer.material.color = Colorholder.thecolor1;
    16. }
    17. if(clothe_ID == 2){
    18.  
    19. renderer.material.color = Colorholder.thecolor2;
    20. }
    21. if(clothe_ID == 3){
    22.  
    23. renderer.material.color = Colorholder.thecolor3;
    24. }
    25.  
    26.  
    27. }
    in every part with the second script place the object holding the first script into the variable for colorholder. in the game object with the 'colorgenerator', assign whatever colors you wish the appropriate objects to become.
     

    Attached Files:

  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Are you showing off an asset? You're in the wrong forum. Check out the showcase or asset forum.
     
  3. eeveelution8

    eeveelution8

    Joined:
    Feb 16, 2013
    Posts:
    8

    Its not an asset, just some coding. If I were to make it an asset I would post it there.
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    So instead you post it in the scripting support forum? Okay. Got it.