Search Unity

Lerping between multiple materials

Discussion in 'Scripting' started by 8r3nd4n, Nov 4, 2011.

  1. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Hi

    I have a few scripts with various different material lerps and they all work fine.
    I now have an plane object (crosshair) that I want to have 4 different materials depending on a timer.
    But the script doesnt work at all. Is it possible to change between multiple materials?
    If not, any suggestions? I can get it to work using GUI but the position changes to where I have the object.

    Cheers

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Crosshair : MonoBehaviour {
    5.    
    6.     public Material materialDefault;
    7.     public Material materialRed;
    8.     public Material materialBlue;
    9.     public Material materialGreen;
    10.     public Material materialYellow;
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.    
    15.          renderer.material = materialDefault;
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.         if(Timing.red)
    22.             renderer.material.Lerp(materialDefault, materialRed, 1.0f);
    23.         else if(Timing.blue)
    24.             renderer.material.Lerp(materialDefault, materialBlue, 1.0f);
    25.         else if(Timing.green)
    26.             renderer.material.Lerp(materialDefault, materialGreen, 1.0f);
    27.         else if(Timing.yellow)
    28.             renderer.material.Lerp(materialDefault, materialYellow, 1.0f);
    29.        
    30.     }
    31. }
    32.  
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Why not just have one material with 4 textures that you can lerp between inside of the shader?
     
  3. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Thanks for the suggestion but I have no experience with shaders.
    Just looking through them now, they cant access script variables can they?
    Because most of my stuff is based around a timing class and all other classes get their data passed from it and the material changes would also require this to work the way I hope