Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cervelx · May 14, 2017 at 02:03 PM · scripting problem

How to change material's alpha of a GameObject ?

Hi Guys ! I've created this script to change light and alpha, the light part is good and work very well but not the alpha... What is the right way ? I put in the public Material a game object with the StandardShader, thank you.

       using UnityEngine;
       using System.Collections;

       public class Light_decrease : MonoBehaviour 
       {

 public Light dL;
 public Material cubeSkyBox;

 void OnTriggerStay ( Collider other)
 {
     if (other.gameObject.CompareTag ("Player")) 
     {
         dL.intensity -= 1 * Time.deltaTime;
         cubeSkyBox.color.a += 1 * Time.deltaTime; 
     }

 }


     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Trevdevs · May 14, 2017 at 03:49 PM

I'm gonna go ahead and assume you are getting this error because your trying to modify the .a aspect of a color itself. It's like trying to change the position.x individually unity hates that for whatever reason.

The proper way to change a color would be this

 void OnTriggerStay ( Collider other)
  {
      if (other.gameObject.CompareTag ("Player")) 
      {
          dL.intensity -= 1 * Time.deltaTime;
 
         Color color = cubeSkyBox.color
         color.a += 1f * Time.deltaTime;
         cubeSkyBox.color = color;
      }
  }

and heres a reference that may be helpful http://answers.unity3d.com/questions/584873/renderermaterialcolora.html hope this helps :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cervelx · May 14, 2017 at 06:24 PM 0
Share

THAN$$anonymous$$ YOU ! It works !

avatar image Cervelx Cervelx · May 14, 2017 at 06:40 PM 0
Share

O$$anonymous$$ but now I've a problem wen i restart the level the material alpha stay at 255 i try to "reset" it whit this but it not work...

      using UnityEngine;
      using System.Collections;

     public class Light_decrease : $$anonymous$$onoBehaviour 
     {

 public Light dL;
 public $$anonymous$$aterial cubeSkyBox;

 private Color color;

 void Awake()
 {
     color = cubeSkyBox.color;
     cubeSkyBox.color = color;
 }

 void Start()
 {
     color.a = 0f;
 }

 void Update()
 {
     if (color.a >= 188f) 
     {
         color.a = 188f;
     }
 }

 void OnTriggerStay ( Collider other)
 {
     if (other.gameObject.CompareTag ("Player")) 
     {
         dL.intensity -= 1 * Time.deltaTime;
         color.a -= 1f * Time.deltaTime;
     }

 }


     }
avatar image Trevdevs Cervelx · May 15, 2017 at 04:58 PM 0
Share

You are setting the color before you change the alpha in the Start function move that line to the start function so it looks like this and it should work.

  void Start()
  {
      color.a = 0f;
      cubeSkyBox.color = color;
  }



Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why my editor vs code opens a few windows when i try to open a script from unity,My VS Code open a few tables when i double click a script in Unity? 0 Answers

Trying to make an object "Flee" from the player but it won't work? 3 Answers

How to make script affect only one this game object 2 Answers

error Int to string?Assets/Scripts/MyConnect.cs(61,60): error CS0029: Cannot implicitly convert type `int' to `string' 1 Answer

How can i limit object dragging area in my inventory? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges