- Home /
Fading Object in and out with timer
Hello,
I have a fast scene that plays and with objects I want to fade in and out at different times...
Simple Right..?
But I'm missing it . Here is my script:
using UnityEngine;
using System.Collections;
public class timer : MonoBehaviour {
public string next;
public float time = 8.0f;
public float time2= 26.0f;
public float time3= 45.0f;
public GameObject One;
public GameObject Two;
public float oneAlpha = 0.0f;
public float twoAlpha = 0.0f;
// Use this for initialization
void Start () {
StartCoroutine("Wait");
}
IEnumerator Wait()
{
if(WaitForSeconds(time))
{
One.GetComponent<Renderer>().material.color = new Vector4(0, 0, 0, oneAlpha);
}
if (WaitForSeconds(time2))
{
Two.GetComponent<Renderer>().material.color = new Vector4(0, 0, 0, twoAlpha);
}
if (WaitForSeconds(time3))
{
Application.LoadLevel(next);
}
}
}
What exactly is it do you want to do with these two object? Do you want to fade each one completely out individually one after the other? Your code is not clear?
@Bentoon You should have told us what the problem is (eg the error you're being given on trying to compile this).
Fortunately one problem at least is easy to spot. You're not using WaitForSeconds properly. To wait for n seconds just put
yield return new WaitForSeconds(n);
See https://docs.unity3d.com/ScriptReference/WaitForSeconds.html. In particular, "WaitForSeconds can only be used with a yield statement in coroutines."
Answer by toddisarockstar · Sep 29, 2016 at 01:22 AM
public class NewBehaviourScript : MonoBehaviour {
public GameObject dude;//here is your object
public float alpha;
public bool sw;
public Color dudescolor;
// Use this for initialization
void Start () {
//must have a transparent shader To work !!!
dude.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
//lets remember the color we started with!!!!
dudescolor =dude.GetComponent<Renderer>().material.color;
}
void Update () {
//play with our float a little bit
if (sw){alpha+=Time.deltaTime;
if(alpha>1){sw=!sw;}}
if(!sw){alpha+=-Time.deltaTime;
if(alpha<0){sw=!sw;}}
//assign the float to your gameobjects alpha!!!
dudescolor.a = alpha;
dude.GetComponent<Renderer>().material.color = dudescolor;
}
}
this is a working example of fading color! im not sure when or how you want to change it but this should include example of everything you need!!! Void update changes ever frame. so use it to change a float a little bit every frame and apply it to a the alpha channel of your color. dont use vector4. use a Color variable instead. and color.a is the alpha channel you are looking for!
@toddisarockstar Thanks Rockstar!
a few questions:
one is what am I doing wrong with the IENumerator to be able to call specific time when the Fade would be triggered
The Second question is I am using models with sub meshes so even if they the shader is set to Trans /or Fade texture so not Lerping like a primitive would because it doesnt aply to children. I hav tried things like Simple LOD to merge the mesh but it doesn't work...Any obvious problemthat I'm not seeing
THAN$$anonymous$$ YOU !
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
public GameObject dude;
public List<Transform> dudeparts = new List<Transform>();
public float alpha;
public bool sw;
public Color dudescolor;
public bool fade;
// Use this for initialization
void Start () {
dudeparts.Add(dude.transform);
foreach (Transform tr in dude.transform) {
print (tr.name);
dudeparts.Add (tr);
tr.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
foreach (Transform tr2 in tr) {
dudeparts.Add (tr2);
tr2.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
}
}
//must have a transparent shader To work !!!
dude.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
//lets remember the color we started with!!!!
dudescolor =dude.GetComponent<Renderer>().material.color;
}
void Update () {
if(Input.Get$$anonymous$$eyDown("space")){whateverfunction();}
//play with our float a little bit
if (sw){if(alpha<1){alpha+=Time.deltaTime;
}}
if (!sw) {
if (alpha > 0) {
alpha += -Time.deltaTime;
}}
//assign the float to your gameobjects alpha!!!
dudescolor.a = alpha;
foreach(Transform tr in dudeparts){
tr.GetComponent<Renderer>().material.color = dudescolor;}
}
void whateverfunction(){
sw = !sw;
}
}
this code would also include the children of the object and those children's children. it's a loop inside of a loop to find the kiddoes.
im not sure why you would need IEnumerator in unity. but here is an example of calling a function when the space bar is pressed. it doesn't matter where or how you change the sw variable in this script. it sill simply fade in or out however or wherever you change the sw variable.
if i want to count off something in time, i personally use Time.deltatime in update. i started on game development and it works a bit different than other types of program$$anonymous$$g. things rely on time as much or more that events. and when thing are triggered, time is also very important to know the order that those things where triggered. the update function is your main tool here in unity! it updates once every visual frame and allows you too keep track of everything in order of when they happen and keeps everything in sync. does that make sence?
for example.... if one object somewhere is destroyed somewhere in the scene or the reference is null even a microsecond before another script calls it, it produces an error and youre crashed!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Timer Between Labels 2 Answers
Making a fill take exactly n seconds to complete 2 Answers
More timer from same class 1 Answer