• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bamabhaa123 · Jul 10, 2013 at 07:42 PM · updatefunctions

My Countdown function seems to go crazy in the Update function but works fine for the first time in the Start function. Help?

This is from the Walker Boys tutorial, game 1.

I have assigned the script to two objects and I want a specified one to as the counter ticks down and then to reappear and change its position when the countdown goes to zero. However, the function I pasted below seems to be working peculiarly. When I apply it in the start() function, it seems to work fine. The object blinks and then reappears in another place, but since it is the start function, it only does that when I start the game. Now, when I put my Countdown1() function (shown below) in the Update() function, the object seems to disregard the counter and starts to appear randomly, switching its position multiple times and refusing to stay still and blink until the counter ends.

Since I am a beginner, I have very much exhausted the knowledge available to me and have been at this for quite a while. Some tips would be great.

 function Countdown1()
 {
     while (countdown2 > 0)
     {
         yield WaitForSeconds(0.3);
         sphereDisappear.renderer.enabled = false;
         yield WaitForSeconds(0.3);
         sphereDisappear.renderer.enabled = true;
         countdown2--;
     }
     
 
     if (countdown2 == 0)
     {
         countdown2 = 3; 
         var newPosition = Vector3(Random.Range(-4, 4), Random.Range(-4, 4), 0);
         sphereDisappear.transform.p    osition = newPosition;
     }
     
 
  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tw1st3d · Jul 10, 2013 at 07:45 PM

The Update() method can not yield. I believe you have to use a Coroutine to do that.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Jul 10, 2013 at 07:54 PM 1
Share

I'm pretty sure you can yield from within Update:

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html (also see various Q&A from UnityAnswers).

Perhaps you're thinking of C#, where you cannot define Update as an IEnumerator.

You don't want to yield from within Update, because you'd be spawning a new Coroutine every, well, update, and that wouldn't be good. That's what this poster is seeing: every Update (30, 60, perhaps more times per second) he/she is starting a new countdown. With many, many countdowns running, yeah, you're going to see weird behaviour.

You need to find a way to start a countdown only when certain conditions are met, so it doesn't launch every frame.

avatar image bamabhaa123 · Jul 11, 2013 at 04:17 PM 0
Share

I tried putting the if statement inside the while loop itself and on first look, it seems to work. However, the teleportation of the object is very inconsistent. At times it seems to teleport after exactly three seconds as written but sometimes it teleports after one or two seconds.

avatar image
0

Answer by fafase · Jul 11, 2013 at 04:21 PM

Your start method only calls once and then no more.

Your update will call this on every frame.

You need to trigger the call when something happens, collisions, state of a variable, input...

 function Update()
 {
    if(something){
       Countdown1();
       reset something to something else to prevent new calls
    }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

when is pysics.raycast used, if it is called in Update() 1 Answer

Trying to write a function that checks if 2 keys are pressed 2 Answers

Regaining system resources? 1 Answer

Question on Update() 1 Answer

Update increment error (2 + 1 = 0?) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges