Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Trying to write a script to do something every 1 second

Discussion in 'Scripting' started by Quintus Iucundus, Aug 18, 2010.

  1. Quintus Iucundus

    Quintus Iucundus

    Joined:
    Aug 6, 2010
    Posts:
    12
    Hi everyone!

    I'm trying to write a script that will instantiate a balloon object randomly. I have my script randomly generating a number between 1 and 20 every 1 second, and if that number is 20, spawn a balloon. However, I can't even get the "every 1 second" part to work! Can anyone point me towards what I'm doing wrong? (probably more than one thing! :-D )

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BalloonEscape : MonoBehaviour {
    6.  
    7.     private int m_Escape;
    8.  
    9.     private void Update() {
    10.         float balloontime = Time.deltaTime;
    11.         balloontime += Time.deltaTime;
    12.        
    13.         if (balloontime >= 1f)
    14.         {
    15.             Randomness();
    16.             Debug.Log("balloontime is" + "" + balloontime);
    17.             balloontime = 0f;
    18.         }
    19.     }
    20.    
    21.     void Randomness()
    22.     {
    23.         m_Escape = Random.Range(1,20);
    24.         Debug.Log("m_Escape is" + "" + m_Escape);
    25.         if (m_Escape == 20)
    26.         {
    27.             //do stuff here
    28.             Debug.Log("Balloon Away!");
    29.         }
    30.     }
    31. }
    32.  
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
  3. Quintus Iucundus

    Quintus Iucundus

    Joined:
    Aug 6, 2010
    Posts:
    12
    Thank you once again!

    Are there any performance hits for using this method? I know infinite while loops can have really bad effects on frame rate. Does this method have any similar problems?
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I don't think you'd find Update() in nearly every Unity scripting example, and in the script templates, if an "infinite while loop" had the effect you're talking about. :wink:

    (An actual infinite loop e.g.
    Code (csharp):
    1. while (true) //no yield instruction here
    will actually be a problem.)

    http://unity3d.com/support/documentation/ScriptReference/YieldInstruction.html
     
  5. jpviegas

    jpviegas

    Joined:
    Jan 12, 2018
    Posts:
    2
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,390
    This post was necro'd so hard the database holding it had to be necro'd to fetch it.
     
    Ryiah and BlackPete like this.
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You are creating a new variable called "balloon time" evey frame and setting it to time.deltatime, you need to declare it outside of any method and only add time.Delta time to it, don't forget to reset it to zero when you spawn a balloon.

    Using a coroutine with "yield return new wait for seconds(60)" or "invoke(" blah",60)" is much better
     
  8. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239
    Invoke(string) is very 2010. State of the art, back then, in method calling technology.