• 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 /
  • Help Room /
avatar image
0
Question by jaklocko · Dec 27, 2015 at 06:57 AM · whilerepeatingadjust

Change InvokeRepeating interval based on amount of game runtime

Hi all, first post,

So, I'm going through some Udemy tutorials on Unity and I'm loving it so far. What I'm working on at the moment is a simple "bomb clicker" game (2D). The premise is simple: bombs fall from the sky, and you have to click them to destroy them before they hit the ground.

Thanks to the tutorial, I have the basic game mechanics down. Bombs fall in a random X range, and between two boundaries, you can click to destroy them, incrementing a counter for how many you hit, and if a bomb gets past the lower boundary and the bomb hits the ground, a counter gets incremented for the one you missed.

This game uses a method within the Start () function called "InvokeRepeating" to make the bombs fall every 1 second right now.

So, here's what I would like to happen, but I am a newb in the coding side of things and I'm failing to get it to work: While the game runtime (how much time has elapsed since you clicked play) is less than 15 seconds, I want bombs to fall at the normal rate of 1 per second. Once 15 seconds have elapsed, I want them to fall at a rate of 1 every half a second, which would imply to me that I just need to adjust the repeatRate of the InvokeRepeating method to 0.5. Then, once 30 seconds have passed, I want bombs to drop even faster. You get the idea. Bombs continually fall faster and faster until you can't click enough. Essentially, once you have missed more than you have hit, I want the game to end. Your score is how long you last.

So, how can I get the InvokeRepeating method to adjust the repeat rate based on how long the game has been running? I've tried while loops, counters, etc. But with the InvokeRepeating method in the Start function, it doesn't seem that I can adjust the repeat rate once the game has started. Any help would be appreciated! Here's my "InstantiateGobj" script:

 #pragma strict
 
 public var gObj : GameObject;
 
 var elapsed : float;
 var fps : float;
 
 
 function Start () {
         InvokeRepeating("CreateRandom", 0, 1);
 }
 
 function CreateRandom () {
     var x : float;
     var y : float;
 
     x = Random.Range(-6.25, 6.3);
     y = 6;
 
     Instantiate( gObj, Vector3(x,y,0), Quaternion.identity);
 }
 
 function Update (){
     elapsed += Time.deltaTime;
     //fps = 60 / Time.deltaTime;
     //Debug.Log("Your FPS: " + fps);
     Debug.Log(elapsed);
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LazyElephant · Dec 27, 2015 at 07:46 AM

The answer is to use Invoke instead of InvokeRepeating. You can simulate the InvokeRepeating functionality by adding another Invoke inside the invoked method.

 function Start () {
          Invoke("CreateRandom", 1);
  }
  
  function CreateRandom () {
      var x : float;
      var y : float;
  
      x = Random.Range(-6.25, 6.3);
      y = 6;
  
      Instantiate( gObj, Vector3(x,y,0), Quaternion.identity);
      Invoke("CreateRandom", WhateverAmountYouWant);
  }
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

33 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 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

continue something if a key is still pressed down? 1 Answer

How to fit a picture into a background GameObject 0 Answers

While loop freeze 0 Answers

How to use infinite loops or alternatively repeated tasks 1 Answer

Rotate around a pivot to a specified degree and stop, with key press 1 Answer

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