Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by eco_bach · Jun 23, 2015 at 10:46 PM · alpha

Fade GameObject alpha with Standard shader

having a tough time trying to do something that should be simple.

How to simply fade a game object that has a Standard shader applied?

This is my code. If I switch the shader to Sprites.default it works.

 public void foo(float val)
     {
         
             _isFade=true;
             float time=2.0f;
             float alphaVal=0.0f;
             LeanTween.alpha(gameObject, alphaVal, time);
         
         Debug.Log("YES calling fade in foo !!!! and gameObject = "+gameObject);
     }
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

4 Replies

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

Answer by TrainWrek · Jun 23, 2015 at 10:58 PM

In the standard shader change rendering mode to "Fade" and the following C# script is an example of fading over time.

 public class FadeAlpha : MonoBehaviour {
     [SerializeField] private float fadePerSecond = 2.5f;
 
     private void Update() {
         var material = GetComponent<Renderer>().material;
         var color = material.color;
 
         material.color = new Color(color.r, color.g, color.b, color.a - (fadePerSecond * Time.deltaTime));
     }
 }
Comment
Add comment · Show 7 · 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 eco_bach · Jun 23, 2015 at 11:15 PM 0
Share

you just saved me another several hours of googling thanks!

avatar image TorQueMoD eco_bach · Feb 05, 2016 at 08:44 AM 0
Share

Hey TrainWrek,

I've tried applying this script to a skeletal mesh object that I have in my scene and I get the error "$$anonymous$$issingComponentException: There is no 'Renderer' attached to the "Sign_Turn 01 Left" game object, but a script is trying to access it."

So I added a skinned mesh renderer to the object (don't know why a skeletal mesh wouldn't have a renderer already assigned to it when I dragged it in from the project window) but it still doesn't work.

Any idea how to solve this?

avatar image t1000 TorQueMoD · Feb 16, 2016 at 11:20 PM 0
Share

I have an asset that I purchased, only the child components have a renderer attached. Every part of the character has a renderer with a texture.

So, you could go for all children of the root transform (parent) and for each object when renderer is not null apply the fade effect.

This other code may help:

http://answers.unity3d.com/questions/225438/slowly-fades-from-opaque-to-alpha.html

See the IEnumerator FadeTo.

avatar image isteffy · Jan 25, 2017 at 11:23 AM 0
Share

All this function does for me is hide the object. no fade.

avatar image Torhque isteffy · Feb 05, 2017 at 12:33 AM 0
Share

The function in the post below did the job for me, but you'll have to update a couple of the lines in order for it to work. I've got a reply on that post detailing which ones.

avatar image JDavidL · Feb 19, 2017 at 12:04 AM 0
Share

Simple and clean! thank you!

avatar image sarynth · Nov 12, 2020 at 05:07 PM 0
Share

This worked great for me. $$anonymous$$ake sure the shader is set to fade. I referenced https://answers.unity.com/questions/1004666/change-material-rendering-mode-in-runtime.html to change from the opaque to fade during runtime so I didn't need two materials.

avatar image
2

Answer by Foysal-Ahmed-Emon · Jun 21, 2016 at 05:03 AM

Set the material : Standard > Fade

Then use this FadeOut3d Function :

 public static IEnumerator FadeOut3D (this Transform t, float targetAlpha, bool isVanish, float duration)
     {
         Renderer sr = t.GetComponent<Renderer> ();
         float diffAlpha = (targetAlpha - sr.material.color.a);
 
         float counter = 0;
         while (counter < duration) {
             float alphaAmount = sr.material.color.a + (Time.deltaTime * diffAlpha) / duration;
             sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, alphaAmount);
 
             counter += Time.deltaTime;
             yield return null;
         }
         sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, targetAlpha);
         if (isVanish) {
             sr.transform.gameObject.SetActive (false);
         }
     }





Then call the FadeOut3d Function form Start :

 using UnityEngine;
 using System.Collections;
 
 public class VanishObject : MonoBehaviour
 {
 
     void Start ()
     {
         
         StartCoroutine (transform.FadeOut3D (0, true, 10));
     }
 
 }
 



The fadeout3d function was write from unity3d default function fadeout which work with SpriteRenderer. But for 3d Object we need Only "Renderer ".

Thanks

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Sep 19, 2016 at 05:52 PM 0
Share

I have a beginner's question: Q: where do I define the FadeOut3D() function?

I'm trying to define it in the same class as the start() that uses it (sbinetFade), but then I get the following error: Assets/sbinetFade.cs(18,31): error CS1106: `sbinetFade.FadeOut3D(this UnityEngine.Transform, float, bool, float)': Extension methods must be defined in a non-generic static class

avatar image Torhque $$anonymous$$ · Jan 20, 2017 at 09:53 PM 0
Share

Update the StartCoroutine line to: StartCoroutine (FadeOut3D (this.transform, 0, true, 10));

And update the parameters list for the FadeOut3D function to: public static IEnumerator FadeOut3D (Transform t, float targetAlpha, bool isVanish, float duration)

avatar image zaxdivine Torhque · Aug 22, 2018 at 02:49 PM 0
Share

All this does for me is to make the object unactive

avatar image
0

Answer by richardzzzarnold · Sep 23, 2016 at 08:45 AM

Foysal Ahmed Emon I am getting the following error with this :

(this UnityEngine.Transform, float, bool, float)': Extension methods must be defined in a non-generic static class

Comment
Add comment · Show 1 · 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 AnomalusUndrdog · Jul 29, 2018 at 12:42 PM 0
Share

What he wrote is an extension method. You need to put that in a new class that's declared as static. The name of the class is entirely up to you (you could call it FadeUtility, for example). Also, you should've wrote your post as a comment to his answer ins$$anonymous$$d of posting it as an answer.

avatar image
0

Answer by jeremypatrickdahan · May 20 at 01:46 PM

A more "no code" option would be : Make the material rendering option "Fade". Create a script "ExposeAlpha" and attach it to your material, with the following code : using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class fade4 : MonoBehaviour
 {
     // Start is called before the first frame update
     [SerializeField] public float alpha = 1.0f;
     private Renderer r;// = GetComponent<Renderer>();
     void Start()
     {
        r  = GetComponent<Renderer>();
     }
 
     // Update is called once per frame
     void Update()
     {
         var material = r.material;
         var color = material.color;
         material.color = new Color(color.r, color.g, color.b, alpha);
     }
 }
 

Now you have an alpha parameter exposed, that you can use in animations.

Comment
Add comment · 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

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

33 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

Related Questions

Unity 4: Image Effect "Bloom" ignoring alpha channel mask 0 Answers

How do you make an X-ray vision following the mouse? 1 Answer

Can't change RGBA shader values in animation clip when I set them to legacy? 0 Answers

Fading between two objects in UnityScript 0 Answers

transparent RGBA PNG textures worked in 4.6, how can I get them working in Unity5? 1 Answer


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