- Home /
The question is answered, right answer was accepted
Object Apear WaitForSeconds Disappear
what's wrong with this script? i'm trying to make it appear when i click the mouse and disappear after 1 second, but the yield WaitForSeconds doesn't work.
function Start ()
{
renderer.enabled = false
}
function Update ()
{
if ( Input.GetMouseButtonDown(0) )
{
renderer.enabled = true;
yield WaitForSeconds (1);
renderer.enabled = false;
}
}
For any help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Read this page : http://answers.unity3d.com/page/newuser.html
Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Update runs every frame, you cannot use Update as a coroutine. I'm surprised you don't have an error in the console saying so.
looking back at this now, I just feel stupid. I can't even believe how bad I was back then.
Answer by NikoBusiness · Jan 07, 2014 at 01:26 PM
you have to put the WaitForSeconds method in a ienumerator so its like this in c#
IEnumerator Example() {
print("start");
yield return new WaitForSeconds(5);
print("end"); //this will apear after 5 seconds
}
and then u call it in start or update function like this
StartCoroutine("Coroutine Name");
and in javascript u just add yield WaitForSeconds in a function like this
function Example () {
print ("stard");
yield WaitForSeconds (5);
print ("end");
}
dont forget to call the function so it works