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 /
This question was closed May 24, 2014 at 07:43 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by plzburnthis · May 23, 2014 at 03:59 PM · c#if-statementsinvokerepeating

Where to run InvokeRepeating?

I would like to invokerepeating and instantiate some prefabs based on the current (and changing) value of BallController.score

 void Start(){
     if (BallController.score>100) 
                {
         InvokeRepeating("Blockers", 1f, 1f);
         }
                 }
 
 void Blockers() {
     if (BallController.score > 100)
         {
         Instantiate(MakinBlockers);
         }
                 } 


Currently my code only starts InvokeRepeating("Blockers") if the game is stopped and started over again with a score of more than 100. Where do I need to put InvokeRepeating to make it check the score more often?

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 Fattie · May 24, 2014 at 07:42 AM 0
Share

the simple answer is YES, START IS AN O$$anonymous$$ PLACE TO PUT IT. that's settled. secondly remove the if line from Start, as Robert tells you to. that's settled. this question is now answered. if you have more problem ask a new question.

avatar image plzburnthis · May 24, 2014 at 09:35 AM 0
Share

I asked where I can put InvokeRepeating to check on it more often, not whether I can put it in start.

Also, I didn't upvote because I don't have enough reputation, I can only mark reply's as answers and I didn't understand yet whether it was a full answer.

I made a forum post incase anyone else needs help with this in the future: http://forum.unity3d.com/threads/247766-How-to-Regularly-Check-on-an-Int-from-another-Script

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · May 23, 2014 at 04:45 PM

If you are doing what I think you are, then you need to remove this condition in Start():

  if (BallController.score>100) 

That is, you want InvokeRepeating() to be called all the time, bu you only Instantiate() if the score is > 100.

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 plzburnthis · May 23, 2014 at 05:55 PM 0
Share

Robertbu, thanks for the reply, I was trying to condense my problem to the purest question, but I think I maybe didn't leave a clear idea of what I'm trying to accomplish.

$$anonymous$$y goal is to use the if operator for both InvokeRepeating and Instantiate because I want to specify the intervals as well as instantiating different Prefabs of $$anonymous$$akinBlockers based on BallController.score.

$$anonymous$$y best guess is that I simply shouldn't be placing the InvokeRepeating in Start() but all places I've tried moving haven't worked yet.

Here is a more detailed example of my script:

 void Start() {
 
                 if (BallController.score >= 1001 && BallController.score <= 3000)
         {
         InvokeRepeating("Blockers", 1f, 1f);
         }
         
                 if (BallController.score >= 3001 && BallController.score <=8000)
         {
         InvokeRepeating("Blockers", 1f, 1f);
                 }
         //this would continue on...
               }
 
 void Blockers() {
 
                 if (BallController.score >= 1001 && BallController.score <= 3000)
              {
             Instantiate($$anonymous$$akinBlockers);
              }
         
         if (BallController.score >= 3001 && BallController.score <=8000)
              {
             Instantiate($$anonymous$$akinBlockers2);
                      }
         //this would also continue on
    }
avatar image robertbu · May 23, 2014 at 09:59 PM 0
Share

If you need to vary the ti$$anonymous$$g, then you don't want InvokeRepeating(). You can use Invoke() and chain the commands, but a coroutine would be a better choice.

 void Start() {
     StartCoroutine($$anonymous$$yInvokeRepeating());
 }
 
 IEnumerator $$anonymous$$yInvokeRepeating() {
     while (true) {
         yield return new WaitForSeconds(waitTime);
         Blockers();
     }
 }

You need to declare waitTime at the top of the file:

 private float waitTime = 1.0f;

Now you can change the wait time between Blockers() calls by changing the value of waitTime.

avatar image plzburnthis · May 24, 2014 at 01:53 AM 0
Share

That doesn't fix the problem I am having, but I went ahead and changed it in the meantime. Thanks, it gives me some new stuff to read about.

$$anonymous$$y problem is still: $$anonymous$$y Prefab isn't Instantiated when score reaches a certain point, but does instantiate on a game restart(you die, (sccore doesn't reset) and restart the game).

I was reading the documentation about OnGUI() this morning and realized that it is calling quite often, in order to update such things, so perhaps I can take advantage of that. I'll let you know what I figure out.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# the position of the object 1 Answer

If gameobject moves do this 1 Answer

if and invoke repeating in start... 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