Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

blinking material

Discussion in 'Scripting' started by vollnull, Aug 26, 2011.

Thread Status:
Not open for further replies.
  1. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    Hey guys,

    I want to make some objects in my scene blink. Until now, I do this with:
    Code (csharp):
    1.  
    2. renderer.material.color = Color.Lerp( Color.white, renderer.material.color, Mathf.Abs( Mathf.Sin( Time.time * 2) ) );
    3.  
    The only problem is, that I am running in a performance issue, if there are many objects, that blink. Is there a way to abandon the sine? I think, that would help already.

    Thanks for any hint.
     
  2. Dreeka

    Dreeka

    Joined:
    Jul 15, 2010
    Posts:
    507
    Of course, there is a much better solution. You can enable/disable it's renderer.

    Code (csharp):
    1. renderer.enabled = false;
    If you want to make it blink, you can do the following:

    Code (csharp):
    1. var blink : boolean = true;
    2.  
    3. function blink()
    4. {
    5.     var myRend:MeshRenderer = GetComponent(MeshRenderer);       //Apply the MeshRenderer component to the myRend variable
    6.     var myState:boolean = true;         //Makes our state true
    7.     while(blink)                        //While our state is true
    8.     {
    9.         myState = !myState;             //Change our state to the oposite
    10.         myRend.enabled = myState;       //Id our state is enabled, the renderer will be enabled. If our state is disabled, the rendeder will be disabled
    11.         yield WaitForSeconds(0.25);     //Waits for 0.25 seconds
    12.     }
    13. }
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    dunno if it is faster, but simply using the time in an algorithm would probably suffice. It would be something like this. (it solves for the in and out time, makes sure its a factor of 2, subtracts 1 then Abs's the result, thus giving you a number that goes from 0 to 1 to 0)

    Code (csharp):
    1.  
    2. var cycleTime=2.0;
    3. private var originalColor : Color;
    4.  
    5. function Start(){
    6.     originalColor=renderer.material.color;
    7.     cycleTime=Mathf.Abs(cycleTime);
    8.     if(cycleTime==0.0)cycleTime=1.0;
    9. }
    10.  
    11. function LateUpdate(){
    12.     var timer=Time.time / cycleTime;
    13.     timer=Mathf.Abs((timer - Mathf.Floor(timer)) * cycleTime - 1);
    14.     renderer.material.color = Color.Lerp( originalColor, Color.white, timer );
    15. }
    16.  
     
  4. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    It is not clear to me, what you do with cycleTime in the Start-Methode, but your LateUpdate-Function is working fine, even with a constant value as cycleTime. Thank you very much.
     
  5. TorQueMoD

    TorQueMoD

    Joined:
    Feb 1, 2010
    Posts:
    16
    @bigmisterb I tried using your code, but it says unexpected symbol : in the second line of code. Using Unity 5.

    *UPDATE*
    Ah, the problem was he said its Csharp code but its actually javascript and outdated javascript at that. The correct Java script code would be:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cycleTime=2.0;
    4. private var originalColor : Color;
    5.  
    6. function Start()
    7. {
    8.     originalColor=GetComponent.<Renderer>().material.color;
    9.     cycleTime=Mathf.Abs(cycleTime);
    10.     if(cycleTime==0.0)cycleTime=1.0;
    11. }
    12.  
    13. function LateUpdate()
    14. {
    15.     var timer=Time.time / cycleTime;
    16.     timer=Mathf.Abs((timer - Mathf.Floor(timer)) * cycleTime - 1);
    17.     GetComponent.<Renderer>().material.color = Color.Lerp( originalColor, Color.white, timer );
    18. }
    Problem is that it doesn't actually work :p
     
    Last edited: Feb 5, 2016
  6. devts2k22

    devts2k22

    Joined:
    Mar 26, 2022
    Posts:
    1
    It works if alpha channel is used.
    originalColor.a = timer;
    GetComponent<Renderer>().material.color = originalColor;
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
Thread Status:
Not open for further replies.