Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Changing Opacity at Runtime

Discussion in 'Scripting' started by BlitheD, Oct 9, 2006.

  1. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    How would I change an object's material's main color opacity through a script at runtime?
    "renderer.material.color.opacity" doesn't work because "'opacity' is not a member of 'UnityEngine.Color'.
    Any ideas?
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    renderer.material.color.a

    You need to set the object to use a Transparent/whatever shader for it to fade...

    color has 4 members, R, G, B, A - in Transparent shaders, A is mapped to opacity
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    The Color class properties and functions are in the documentation. As Nicholas said, 'a' property is the "alpha" (or "opacity").
     
    WonderDaf likes this.
  4. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    Thanks guys. I actually looked at that page but was so set on finding "opacity" that I missed the alpha reference.
    However, when I tried this script it did not work:
    GameObject.Find("someObject").renderer.material.color.a = 100;
    Unity says "Assigment to temporary". I don't get what that means or how to fix it.
    Any ideas?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Are you using C#? In C# it is not possible to modify structs inplace when accessed through a property. Thus you would have to set the entire Color at once.
    Code (csharp):
    1.  
    2. GameObject.Find("someObject").renderer.material.color = Color (1,1,1,0.5);
    3.  
    Or use javascript, where this is of course possible.
    Also the alpha value is a float thus goes from 0 to 1 .
     
  6. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    Actually, I am using javascript and I tried using
    GameObject.Find("someObject").renderer.material.color.a = 1;
    and got the same message...
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can split it out into two lines like this:
    Code (csharp):
    1.  
    2. function Start ()
    3. {
    4.    var go = GameObject.Find("someObject");
    5.    go.renderer.material.color.a = .5;
    6. }
    7.  
     
  8. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    That worked. Thank you! I'm very curious as to why, though. I'm trying to learn enough about programming to create stuff with Unity but there's so much that is just dark water to me.
    Anyways, at least I know how to fix it now. Thanks again.