Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Change color of generated object

Discussion in 'Scripting' started by TrueAncalagon, May 11, 2020.

  1. TrueAncalagon

    TrueAncalagon

    Joined:
    Apr 23, 2020
    Posts:
    8
    Hi guys, I have a problem that may seems easy to you but I had tried a lots of suggestion found online. The main problem is that I'm still learning of course and I don't understand all the mechanics. This is my first 100% game build by myself and I'm trying to find a solution to probably a problem that I had made inside my logics. I'm pretty sure the solution is simple but...

    Down you can see a portion of my script related to creation of rectangles with a random color from my array.

    Code (CSharp):
    1.  
    2. "brickPrefab class"
    3.  
    4. // variables
    5. public Color[] colorPicker;
    6. private Color selectedColor;  
    7.  
    8. private void Start(){
    9.       int randomColor = Random.Range(0, colorPicker.Length);
    10.       selectedColor = GetComponent<Renderer>().material.color = colorPicker[randomColor];
    11. }
    12.  
    13. private void GetHit(){
    14.       // change color of the rectangle here
    15. }
    16.  
    In my GameManager script

    Code (CSharp):
    1. public GameObject brickPrefab;      // brick
    2.  
    3. private void Start()
    4. {
    5.       // bricks generator
    6.         for (int i = -4; i < 5; i++)
    7.         {
    8.             for (int z = -2; z < 2; z++)
    9.             {
    10.                 Instantiate(brickPrefab, new Vector3(i * 2, 0.25f, z * 1.2f), Quaternion.identity);
    11.             }
    12.         }
    13. }
    Till this point I'm pretty happy, the game works fine. Now I want that the rectangles changes color (maybe alpha) when hit by enemy (method in brickPrefab class, GetHit() ). And I can't make it work. My prefab is a rectangle wit a general material with white color on it.

    Do you have any suggestion?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You can just set the renderer's material.color again to change the color in your GetHit() method.
     
  3. TrueAncalagon

    TrueAncalagon

    Joined:
    Apr 23, 2020
    Posts:
    8
    Ah yes, I can write inside GetHIt method

    Code (CSharp):
    1. this.GetComponent<Renderer>().material.color = Color.black;
    and it will work. But I would like to have 2-3 state od changes.

    0 hit = 100% alpha
    1 hit = 75 % alpha
    ...
    ...

    The problem is that I don't know how to change the alpha of the color. On screen I have, let's say, 40 rectangles and every one have a different colour. Of course I can reassign a random colour to the rectangle when hitted but I will preffer an alpha change to indicate a state o decay
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    That part is simple. Basically you just need to make a new color with a different alpha value (which you can base off another color):
    Code (CSharp):
    1. public static Color WithAlpha(Color color, float a) {
    2.     return new Color(color.r, color.g, color.b, a);
    3. }
    Then just assign the new color to the renderer's material. Just remember that the alpha value (and r,g,b) should be a number between 0 and 1.
     
  5. TrueAncalagon

    TrueAncalagon

    Joined:
    Apr 23, 2020
    Posts:
    8

    I've tied to but indeed I had made some mistake.

    Code (CSharp):
    1. private void GetHit()
    2. {
    3. // get original material
    4.             Color originalColor = GetComponent<Renderer>().material.color;
    5. // set new color based on new method
    6.             this.GetComponent<Renderer>().material.color = WithAlpha(originalColor, 0.5f);*/
    7. }
    8.  
    9. public static Color WithAlpha(Color color, float a)
    10.     {
    11.         return new Color(color.r, color.g, color.b, a);
    12.     }
    Nothing happen when the rectangle is hit
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I see a "*/" at the end of line 6 of your code snippet. Either that's a copy/paste error, or all of your code up to that point is part of a large block comment. If it's part of a comment, it won't run.
     
  7. TrueAncalagon

    TrueAncalagon

    Joined:
    Apr 23, 2020
    Posts:
    8
    Sorry the */ is an error for testing. The actual code don't have it.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Does that code actually run? If you put a Debug.Log() statement around there, does it show up in the logs?
     
  9. TrueAncalagon

    TrueAncalagon

    Joined:
    Apr 23, 2020
    Posts:
    8
    Yes, if I put a Debug.log() insite WithAlpha method I get a log every time the rectangle get hit. Maybe something is wrong with the method to get the original color and than put it back to the same rectangle