Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Unity crashes when using while loop 0 Answers

how to display object while ! raycast hit is true ? 2 Answers

Consistent spacing between gameobjects when some disabled 1 Answer

Stucked in a infinite loop and don't know why 2 Answers

Looking for advice about how to streamline my code (avoid repetition) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges