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 /
This question was closed Feb 15, 2018 at 01:40 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by plumel · Mar 22, 2016 at 07:04 PM · speedforceinvokerepeatingspeed up

InvokeRepeating not working

Hi guys,

So I'm trying to make the speed of my shuriken increase by 2 after 30 seconds, and then every 30 seconds after that. From what I read in the documentation, this should be how it's done?

 private Rigidbody2D rb2d;
 public float speed = 10f;
 public GameObject DownShuriken;
 public static int livess = 2; 
 void Start ()
 {
     rb2d = GetComponent<Rigidbody2D> ();
 }
 void Update () 
 {
     rb2d.AddForce(Vector2.down*speed);
     InvokeRepeating ("IncreaseSpeed", 30, 30f);
 }
 void SpawnShurikenDown()
 {
     Vector3 RandomSpawn = new Vector3 (Random.Range (-9.1F, 9.1F), Random.Range (5.04F, 5.04F),1F);
     Instantiate (DownShuriken, RandomSpawn, Quaternion.identity);
 }

 void OnTriggerExit2D(Collider2D other)
 {
     if(other.gameObject.CompareTag ("BottomBackground"))
     {
         SpawnShurikenDown ();
         Destroy (DownShuriken);
     }
 }
 void IncreaseSpeed ()
 {
     speed = speed + 2f;
 }

}

After 30 seconds, the speed of the shuriken does not change. A Debug.LogError (speed) does nothing inside the function IncreaseSpeed when put beneath the line where I change the speed.

Comment
Add comment · Show 5
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 spooneystone · Mar 23, 2016 at 11:00 AM 0
Share

Speed is public is anything else accessing it?

avatar image plumel spooneystone · Mar 23, 2016 at 11:15 AM 0
Share

I use the variable name speed in other scripts but nothing else is accessing it in this script.

avatar image plumel spooneystone · Mar 23, 2016 at 11:15 AM 0
Share

Should I put it to private?

avatar image spooneystone · Mar 23, 2016 at 12:36 PM 0
Share

Try just Invoke to see if that works but I'm certain you can't call invoke or invoke repeating in update anyway because it would call it every frame and cause chaos!

avatar image spooneystone · Mar 23, 2016 at 12:41 PM 0
Share

Also one of the floats in the invoke repeating is declared at a float with f but thaw other isn't . That might be causing some craziness there .

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Toon_Werawat · Mar 22, 2016 at 08:20 PM

Maybe update was reset InvokeRepeating?

If so. You can try alternative.

By replace

 void Update () 
 {
     rb2d.AddForce(Vector2.down*speed);
     InvokeRepeating ("IncreaseSpeed", 30, 30f);
 }

And With this script

 public float lastTime;
 void Update () 
 {
     rb2d.AddForce(Vector2.down*speed);
     if (Time.time > lastTime)
     {
         lastTime = Time.time + 30;
         IncreaseSpeed();
     }
 }

Comment
Add comment · Show 6 · 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 plumel · Mar 22, 2016 at 08:38 PM 0
Share

This does work, thank you.

But I don't understand.. If InvokeRepeating was being reset by Update(), would I be able to call InvokeRepeating else where? like Start()?

avatar image Toon_Werawat plumel · Mar 22, 2016 at 08:44 PM 0
Share

Yes. Another place that not update. (Or Fixed/Late update)

avatar image plumel Toon_Werawat · Mar 22, 2016 at 08:47 PM 0
Share

For some reason that doesn't work though?

     private Rigidbody2D rb2d;
     public float speed;
     public GameObject DownShuriken;
     public static int livess = 2; 
     void Start ()
     {
         rb2d = GetComponent<Rigidbody2D> ();
         InvokeRepeating ("IncreaseSpeed", 30, 30f);
     }
     void Update () 
     {
         rb2d.AddForce(Vector2.down*speed);
     }
     void SpawnShurikenDown()
     {
         Vector3 RandomSpawn = new Vector3 (Random.Range (-9.1F, 9.1F), Random.Range (5.04F, 5.04F),1F);
         Instantiate (DownShuriken, RandomSpawn, Quaternion.identity);
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         if(other.gameObject.CompareTag ("BottomBackground"))
         {
             SpawnShurikenDown ();
             Destroy (DownShuriken);
         }
     }
     void IncreaseSpeed ()
     {
         speed = speed + 2;
     }
 }

avatar image Toon_Werawat · Mar 23, 2016 at 07:37 AM 1
Share

Hm? $$anonymous$$aybe I should try it on my PC. And see what it does

avatar image plumel Toon_Werawat · Mar 23, 2016 at 10:45 AM 0
Share

Any luck with that?

avatar image Toon_Werawat plumel · Mar 23, 2016 at 01:00 PM 0
Share

Did you try to make item Spawn everytime it exit trigger? Try to make sure. You tick "is$$anonymous$$inematic" And at trigger. It have Collider2D with tick "isTrigger" Just asking. I'm testing here. And It not moving at all. ._,

Follow this Question

Answers Answers and Comments

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

Adding Speed 0 Answers

accelerate speed 0 Answers

How to include speed trigger creation option in this script. 0 Answers

Getting two Objects to the same direction with different speed 0 Answers

How to make sphere a cricket ball? 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