How do we make a blinking objects?

Hello All,

I’m new to programming. And I’m following the walkerboystudio courseLab 1. And I’m stuck creating a blinking object.

I’ve got this cube has an instantiate for explosion and then blinking that never last =.=

it should be blinking 1’st, then explode and then sets up a new position for the sphere…
Anyone can help me??I don’t seem able to find the errors… :((
Everything is fine until I add in the code for blinking the sphere. arrrggghhh
Seniors!!! HELP ME please… :slight_smile:

Here is my update script and the blinking script

function Update ()
{
if (numberOfClicks <=0)
{
Instantiate (explosion,transform.position,transform.rotation); //creates an explosion
var enemyPos = Vector3(Random.Range(-4.5,4.5),Random.Range(-4.5,4.5),0); //new random position for GO
Blink(blinkCount); //blink the sphere
RespawnWaitTime();
if (blinkCount==0)
{transform.position = enemyPos; //relocate the game object to new location
numberOfClicks = storeClicks;

}
}

}

function Blink(i)
{
for( --i; i>0;)
{
renderer.enabled = false;
yield WaitForSeconds(0.2);
renderer.enabled = true;
yield WaitForSeconds(0.2);
}

}

Welcome to the forums, please use code tags when posting code.

To answer your question, you would simply use an InvokeRepeating method to start immiately and repeat every 0.4 seconds…

InvokeRepeating("Blink", 0, 0.4);

function Blink()
{
	renderer.enabled = false;
	yield WaitForSeconds(0.2);
	renderer.enabled = true;
}

If you are looking to make it blink for a finite amount of time, I would use a StartCoroutine and feed it the time.

StartCoroutine(Blink(2.0)); 

function Blink(waitTime : float) {
	var endTime=Time.time + waitTime;
	while(Time.time<waitTime){
		renderer.enabled = false;
		yield WaitForSeconds(0.2);
		renderer.enabled = true;
		yield WaitForSeconds(0.2);
	}
}

Tq for your solution :slight_smile:

a bit late , is what you can do also but this rejoin other comment, to define a number of time you want blink well you can add a count variable that you set each time you pass in the invoked function and check upon this.

PS>> added a counter …well hope that help :slight_smile:

using UnityEngine;
using System.Collections;

public class ToggleRender : MonoBehaviour
{
	public float Duration = 3f;	
	public bool State = true;	
	public bool Repeat = false;	
	public int RepeatTime = 0;

        int repeatTime = 0;
	
	void OnEnable()
	{
		if(!renderer)
		{
			enabled = false;
			return;
		}

               // make sure we dont have negative counter set
		RepeatTime = Mathf.Abs(RepeatTime);

               // store RepeatTime for reset 
		repeatTime = RepeatTime;
		
		renderer.enabled = State;
		if(!Repeat)
			Invoke("Activate", Duration);
		else
			InvokeRepeating("ActivateRepeating", Duration, Duration);
	}
	
	void Activate()
	{
		renderer.enabled = !State;
		enabled = false;
	}
	
	void ActivateRepeating()
	{
		State = !State;
		renderer.enabled = State;
		
		if(CheckCount())
		{
			CancelInvoke();
                        RepeatTime = repeatTime;
			enabled = false;
		}
	}
	
	bool CheckCount()
	{
		RepeatTime --;
		
		if(RepeatTime == 0)
			return true;
		else
			return false;
	}
}

I think bigmisterb meant to put this as the while loop condition:

while(Time.time<endTime)

Well, if you’re looking for a really simple blinking object, you could just use the “%” operand

void Update () {
		if(Time.fixedTime%.5<.2)
		{
			renderer.enabled=false;
		}
		else{
			renderer.enabled=true;
		}
	}

or Simply use a cos function on Alpha. Ex :

float blinkingAlpha = (0.5f + 0.5f * Mathf.Cos(timeForColorBlink)) ;
Gizmos.color = new Color(traceColor.r, traceColor.g, traceColor.b, blinkingAlpha);
timeForColorBlink+=0.25f;

dude, don’t make your life harder :slight_smile:

Somehow, I dont think that the guy who posted 2 posts since, January of 2014 is going to respond to you. :wink:

Is there a way we could turn this int a public bool or public int? so it is easier to change speed and you wouldn’t need to make copies of the script.

The concept is fairly simple. This is an implementation with a differing off and on state.

public bool state;
public float onDuration = 1;
public float offDuration = 1;
private float nextSwitch;

void Update(){
if(Time.time > nextSwitch){
state = !state;
nextSwitch += (state ? onDuration : offDuration;
}
}