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);
    }

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));
	}
}

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

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

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.