- Home /
How Do i make the prefab stop blinking
I am trying to get my prefab to stop blinking after 1.5 seconds. How do I do this. Time is still kind of confusing for me as I am really new to unity. Any help would be greatly appreciated.
using UnityEngine;
using System.Collections;
public class JmpFlasher : MonoBehaviour {
Material mat;
public Color color;
bool isFlashing;
// Use this for initialization
void Start () {
isFlashing = false;
mat = new Material (Shader.Find("Transparent/Diffuse"));
mat.color = new Vector4 (color.r,color.g,color.b,1);
renderer.material = mat;
}
// Update is called once per frame
void Update () {
if(isFlashing)
mat.color = new Vector4 (color.r,color.g,color.b,(Mathf.Sin (Time.time*8+1)/2));
}
public void startFlash (){
isFlashing = true;
}
}
Answer by Landern · Aug 28, 2014 at 06:14 PM
public IEnumerator startFlash (){
isFlashing = true;
yield return new WaitForSeconds(15);
isFlashing = false;
}
I put that in my script to replace the public void startFlash()section of the code but it does not flash at all. is that the wrong place to put it?
Thank you
void update()
{
startFlash();
if(isFlashing)
mat.color = new Vector4 (color.r,color.g,color.b,($$anonymous$$athf.Sin (Time.time*8+1)/2));
}
public IEnumerator startFlash (){
isFlashing = true;
yield return new WaitForSeconds(15);
isFlashing = false;
}
That should work.
$$anonymous$$mm correct me if I'm wrong but don't you have to call all IEnumerator functions with:
StartCoroutine(IEnumerator routine);
??
Answer by michael0321 · Aug 28, 2014 at 10:47 PM
Thanks everyone i'll give it a try
Thank for the help but the code only turns my prefab into the the color it is supposed to flash and stays that color. here is the code
using UnityEngine; using System.Collections;
public class JmpFlasher : $$anonymous$$onoBehaviour {
$$anonymous$$aterial mat;
public Color color;
public bool isFlashing;
// Use this for initialization
void Start () {
isFlashing = false;
mat = new $$anonymous$$aterial (Shader.Find("Transparent/Diffuse"));
mat.color = new Vector4 (color.r,color.g,color.b,1);
renderer.material = mat;
}
// Update is called once per frame
//void Update () {
// if(isFlashing)
// mat.color = new Vector4 (color.r,color.g,color.b,($$anonymous$$athf.Sin (Time.time*8+1)/2));
//}
//public void startFlash (){
// isFlashing = true;
//}
void Update()
{
startFlash();
if(isFlashing)
mat.color = new Vector4 (color.r,color.g,color.b,($$anonymous$$athf.Sin (Time.time*8+1)/2));
}
public IEnumerator startFlash (){
isFlashing = true;
yield return new WaitForSeconds(2);
isFlashing = false;
}
}
Hello again!
Well I think it is because you never change the value of isFlashing
. I down know but I guess it is 'cause you have to call it like I said with StartCoroutine()
so I would try this:
public class JmpFlasher : $$anonymous$$onoBehaviour {
$$anonymous$$aterial mat;
public Color color;
public bool isFlashing;
// Use this for initialization
void Start () {
isFlashing = false;
mat = new $$anonymous$$aterial (Shader.Find("Transparent/Diffuse"));
mat.color = new Vector4 (color.r,color.g,color.b,1);
renderer.material = mat;
StartCoroutine(startFlash());
}
void Update()
{
if(isFlashing)
mat.color = new Vector4 (color.r,color.g,color.b,($$anonymous$$athf.Sin (Time.time*8+1)/2));
}
public IEnumerator startFlash (){
isFlashing = true;
yield return new WaitForSeconds(2.0f);
isFlashing = false;
}
}
And I think that would be all... I hope this works! good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220613161757im_/https://answers.unity.com/themes/thub/images/avi.jpg)