Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by pdunton · Feb 06, 2014 at 05:00 AM · javascriptfunctioninvokerepeating

How to create a faster and faster function call?

I have a script that creates a coin at rate seconds and every rate seconds, rate is decreased by fasterRate. This should create the coins faster and faster, but the InvokeRepeating() called with rate is only called at start so that it doesnt get faster and faster but just stays the same rate. I need a way to call CreateCoin() faster and faster. Thanks!

 #pragma strict
 var xpos : float;
 var spawnpoint : Vector3;
 var coin : GameObject;
 var rotation : Quaternion;
 rotation.eulerAngles = new Vector3(0, 0, 0);
 var newCoin :Object;
 var rate : float = 3.0;
 var fasterRate : float = 0.1;
 
 function Start () {
 InvokeRepeating("CreateCoin", 1, rate);
 }
 
 function Update () {
 Destroy(GameObject.FindWithTag("Coin"),10);
 
 }
 
 function CreateCoin(){
     xpos = Random.Range(-50.0,50.0);
     spawnpoint = new Vector3(xpos, 80, 0);
     
     var myColor : Color;                       // This is just working for another part of my script, Please Ignore.
     var mode : boolean = Random.value > 0.5f;
     var rand : float = Random.value;
     var index : int = Random.Range( 0, 3 ); 
     myColor[index] = rand;
     myColor[( index + ( mode ? 1 : 2 ) ) % 3] = 1;
     myColor[( index + ( mode ? 2 : 1 ) ) % 3] = 0;
     
     newCoin = Instantiate(coin, spawnpoint, Quaternion.identity);
     //newCoin = newCoin as GameObject; 
     //newCoin.renderer.material.color = myColor;
     
     rate = rate - fasterRate;
 }
 
 
 
Comment
Add comment · Show 2
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 RyanZimmerman87 · Feb 06, 2014 at 05:07 AM 0
Share

I'm a big fan of StartCoroutine and IEnumerator for this kind of thing. Lets you have a lot of control of exactly when things should occur.

Seems there are some differences with C# and JavaScript for this stuff so hopefully those work in JavaScript too.

avatar image Eric5h5 · Feb 06, 2014 at 05:13 AM 0
Share

There's no differences with C# and Javascript with the use of coroutines, only syntax.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Feb 06, 2014 at 05:06 AM

Rather than use InvokeRepeating(), just use Invoke() at the end of CreateCoin(). Insert between line 36 and 37:

 Invoke("CreateCoin", rate);

and in Start():

 Invoke("CreateCoin", rate);
Comment
Add comment · Show 3 · 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 Eric5h5 · Feb 06, 2014 at 05:15 AM 0
Share

I'd be a little concerned about using recursion unnecessarily, if it even works with Invoke (haven't tested).

avatar image robertbu · Feb 06, 2014 at 05:40 AM 1
Share

@Eric5h5 - I agree your solution is better, though chaining Invoke() does work. To me this is not recursion since the code returns from CreateCoin() before the next Invoke() is executed.

avatar image pdunton · Feb 07, 2014 at 04:05 AM 0
Share

This seems like the best option for me, and after some testing, works great. Thanks!

avatar image
0

Answer by Eric5h5 · Feb 06, 2014 at 05:12 AM

It's simplest to use a coroutine in a loop, along with WaitForSeconds:

 function Start() {
     while (true) {
         CreateCoin();
         yield WaitForSeconds(rate);
         rate -= .1;
     }
 }

Although you'd want to put in some condition for making rate not go too low.

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
avatar image
0

Answer by NDJoshi · Mar 31, 2014 at 01:59 PM

I think you should use coroutine();

And pass a variable in hold time(yield). In Update() decrease this variable.

Try this and you will get the exact result you want. If is doesn't, reply me here.

Comment
Add comment · Show 1 · 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 Eric5h5 · Mar 31, 2014 at 05:02 PM 0
Share

There's no reason why you would use Update at all. See my answer.

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

21 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

Related Questions

How to use functions between two scripts 2 Answers

OnCollisionEnter does not work. 1 Answer

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

Restart this script 3 Answers

Camera Move 1 Answer


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