Search Unity

Mathf.Lerp not working in the Awake function?

Discussion in 'Scripting' started by Jeepster, Jun 17, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    I'm trying to get a blood effect for the camera every time my player is hit, by changing the material of a red colored material that's on a plane in from of the camera at run time.

    My code works in the Update function, but since it has to happen every time my player is shot (which will happen a lot and in quick succession) I need the material to reset and restart after having been shot.

    This is my code:
    Code (CSharp):
    1.   void Awake()
    2.     {
    3.         //time = 0;
    4.         //Color c = rend.material.GetColor("_TintColor");
    5.         //c = new Color(1f, 1f, 1f, 1f);
    6.  
    7.         StartCoroutine(BloodFx());
    8. }
    9.  
    10.     }
    11.     IEnumerator BloodFx()
    12.     {
    13.  
    14.         yield return new WaitForSeconds(0f);
    15.         Color c = rend.material.GetColor("_TintColor");
    16.         c.a = Mathf.Lerp(.20f, 0.0f, Time.time);
    17.         rend.material.SetColor("_TintColor", c);
    18.     }
    As you can see I've tried solving my problem with a coroutine that is called on Awake, but my Mathf.Lerp only works in the Update function not on Awake and only once (It doesn't reset).

    Does anybody know how I could get this to work as I want it to? I'd really appreciate the help, thanks.
     
  2. Deleted User

    Deleted User

    Guest

    Does this compile? What is this } on line 8 for?

    To get useful help, you need to post the entire script. :)
     
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    You're right, I added a bracket too many after pasting the original code by mistake. Here's the whole script. There's really nothing else there:
    Code (CSharp):
    1.  public Renderer rend;
    2.     float time;
    3.  
    4.     public Material[] materials;
    5.     public GameObject parent;
    6.  
    7.     void Awake()
    8.     {
    9.         //time = 0;
    10.         //Color c = rend.material.GetColor("_TintColor");
    11.         //c = new Color(1f, 1f, 1f, 1f);
    12.         StartCoroutine(BloodFx());
    13.  
    14.     }
    15.     IEnumerator BloodFx()
    16.     {
    17.  
    18.         yield return new WaitForSeconds(0f);
    19.         Color c = rend.material.GetColor("_TintColor");
    20.         c.a = Mathf.Lerp(.20f, 0.0f, Time.time);
    21.         rend.material.SetColor("_TintColor", c);
    22.     }
    23.     void Update()
    24.     {
    25.      
    26.             //Color c = rend.material.GetColor("_TintColor");
    27.             //c.a = Mathf.Lerp(.20f, 0.0f, Time.time);
    28.             //rend.material.SetColor("_TintColor", c);
    29.        
    30.      
    31.      
    32.         //time += 0.08f * Time.deltaTime;
    33.         //RateOverTime = Mathf.Lerp(0f, 1f, time);
    34.         //rend.materials[0].SetFloat("_Cutoff", RateOverTime);
    35.  
    36.  
    37.  
    38.     }
    39. }