blinking material

Hey guys,

I want to make some objects in my scene blink. Until now, I do this with:

renderer.material.color = Color.Lerp( Color.white, renderer.material.color, Mathf.Abs( Mathf.Sin( Time.time * 2) ) );

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.

Of course, there is a much better solution. You can enable/disable it’s renderer.

renderer.enabled = false;

If you want to make it blink, you can do the following:

var blink : boolean = true;

function blink()
{
    var myRend:MeshRenderer = GetComponent(MeshRenderer);		//Apply the MeshRenderer component to the myRend variable
	var myState:boolean = true;			//Makes our state true
	while(blink)						//While our state is true
	{
		myState = !myState;				//Change our state to the oposite
		myRend.enabled = myState;		//Id our state is enabled, the renderer will be enabled. If our state is disabled, the rendeder will be disabled
		yield WaitForSeconds(0.25);		//Waits for 0.25 seconds
	}
}

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)

var cycleTime=2.0;
private var originalColor : Color;

function Start(){
	originalColor=renderer.material.color;
	cycleTime=Mathf.Abs(cycleTime);
	if(cycleTime==0.0)cycleTime=1.0;
}

function LateUpdate(){
	var timer=Time.time / cycleTime;
	timer=Mathf.Abs((timer - Mathf.Floor(timer)) * cycleTime - 1);
	renderer.material.color = Color.Lerp( originalColor, Color.white, timer );
}

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.

@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:

#pragma strict

var cycleTime=2.0;
private var originalColor : Color;

function Start()
{
    originalColor=GetComponent.<Renderer>().material.color;
    cycleTime=Mathf.Abs(cycleTime);
    if(cycleTime==0.0)cycleTime=1.0;
}

function LateUpdate()
{
    var timer=Time.time / cycleTime;
    timer=Mathf.Abs((timer - Mathf.Floor(timer)) * cycleTime - 1);
    GetComponent.<Renderer>().material.color = Color.Lerp( originalColor, Color.white, timer );
}

Problem is that it doesn’t actually work :stuck_out_tongue:

It works if alpha channel is used.
originalColor.a = timer;
GetComponent().material.color = originalColor;

This thread is 12 years old and doesn’t need necroing. Please don’t do this.

https://discussions.unity.com/t/757848

Thanks.