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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Setting transparency in script?

Discussion in 'Scripting' started by RoxieCotton, Jan 31, 2018.

  1. RoxieCotton

    RoxieCotton

    Joined:
    Jan 16, 2018
    Posts:
    4
    I am learning via youtube and reading, I know there is a c# line that goes right in here to set the transparency when i am changing the color, but I can find no examples or explanations via google and forums. This is the snippet of code that is all in all working properly, but i want to change the transparency as I am changing colors.


    void Update ()
    {
    if (current)
    {
    GetComponent<Renderer>().material.color = Color.magenta;
    }
    else if (target)
    {
    GetComponent<Renderer>().material.color = Color.green;
    }
    else if (selectable)
    {
    GetComponent<Renderer>().material.color = Color.red;
    }
    else
    {
    GetComponent<Renderer>().material.color = Color.white;
    }
    }

    I found this as a old example from 2010 but it did not work.

    other.renderer.material.color.a = 0.5; // 50 % transparent

    So that pretty much explains it sorry for the noob question, I searched all over the renderer.API stuff with no luck.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You don't need to constantly get your renderer in update like that. Just name it and get it in start:
    Code (csharp):
    1.  
    2.     Renderer renderer;
    3.     void Start()
    4.     {
    5.         renderer = GetComponent<Renderer>();
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         if (true)
    11.         {
    12.             renderer.material.color = Color.magenta;
    13.             Color color = renderer.material.color;
    14.             color.a = .3f;
    15.             renderer.material.color = color;
    16.         }
    17.     }
    18. }
    19.  
    You also need to set your material to transparent in the editor by clicking on the little arrow under Rendering Mode.
     
  3. RoxieCotton

    RoxieCotton

    Joined:
    Jan 16, 2018
    Posts:
    4
    its for a turn based game so the tile colors change every turn based on what unit you have highlighted. Ill try this tho and see if it works thank you!!
     
  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    In that case you should not use the update method. Instead you should send a message when the player clicks on hte object. I don't want to confuse you too much if you just began but it never hurts to get the right direction.

    I assume that at some point you assign the current variable for example if hte player clicks on it. This might be another script. Then you can do this:
    Code (CSharp):
    1.  
    2. //Put this on top of oyur manager class:
    3. [SerializeField] Color NormalColor;
    4. [SerializeField] Color CurrentColor;
    5. [SerializeField] Color selectableColor;
    6. [SerializeField] Color TargetColor;
    7.  
    8.  
    9. //Wherever you assign states:
    10.  
    11. //Instead of
    12. tile.current = true;
    13. //Use this:
    14. tile.SetColor(CurrentColor);
    15.  
    16. //Instead of
    17. tile.target = true;
    18. //Use this:
    19. tile.SetColor(TargetColor);
    20.  
    21. //Instead of
    22. tile.selectable = true;
    23. //Use this:
    24. tile.SetColor(SelectableColor);
    25.  
    26. //Make sure to deselect the old objects:
    27. //Instead of
    28. tile.current = false;
    29. //Use this:
    30. tile.SetColor(NormalColor);
    YOu then need a method in your tile class called SetColor:


    Code (CSharp):
    1. public void SetColor(Color color)
    2. {
    3.     renderer.material.color = color;
    4. }
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    Did you perhaps mean "if (renderer != null)", or?
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    He had some boolean in there, I just took a piece of the code to show how to do the things necessary. That's kind of a problem with using code tags because it looks like it should be replacement code, but wasn't meant for that purpose. It was just an example on how to show transparency.