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 GH0STfdd · Apr 02, 2015 at 09:14 PM · c#functionloop

Loop a function in c#.HELP!!!

I want to when i will press "Buy Dealer" button,function Deal will loop.Every 3 seconds Deal function need to add +20$ and -1g meth...

...So,when i apply this script unity is crashing...How to loop a function,please help and sorry for my broken english.

 function Update()
     {
       if(dealers >=1)
        {
           if(methScript.meth >=1)
             {
                while(true)
                 {
                   Deal();
                 }
             }
        } 
     }
     
     IEnumerator time()
     {
       yield return new WaitForSeconds(3f);
     }

          void Deal()
 {
                        moneyScript.money +=20;
                        methScript.meth -=1;
                        StartCoroutine(time());
 }


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 ian_sorbello · Apr 02, 2015 at 09:18 PM 0
Share

You're using your coroutine wrong - only the time() function waits for 3 seconds before it re-commences - your Update() function will still get called thousands of times. This then starts another co-routine - and so you're running out of stack space, hence why unity is crashing.

avatar image maccabbe · Apr 02, 2015 at 09:31 PM 0
Share

Here is a unity tutorial about loops http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops which is part of a larger series that you should probably learn http://unity3d.com/learn/tutorials/modules/beginner/scripting

The reason unit is crashing is because you have an infinite loop, while(true). As a result, your program will repeatedly evaluate the condition and inside of the loop forever, without any breaks to update the display or unity itself.

2 Replies

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

Answer by _joe_ · Apr 02, 2015 at 09:22 PM

Careful, you're mixing C# and JS in your code... It's always best to use Coroutines instead of Update, so I wrote the basic coroutine, add your conditions and you'll be set.

Here you go:

 void Start () {
     StartCoroutine("Deal");
 }
 
 IEnumerator Deal(){
     while(true){
         moneyScript.money +=20;
         methScript.meth -=1;
 
         yield return new WaitForSeconds(3);
     }
 }
Comment
Add comment · Show 2 · 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 GH0STfdd · Apr 03, 2015 at 06:20 PM 0
Share

yes it works,but i need loop this coroutine when i will press Buy Dealer button,not from the start.

avatar image GH0STfdd · Apr 03, 2015 at 06:32 PM 0
Share

Ooops,ok i fixed it :)))) Thank you very much!

avatar image
-1

Answer by UNZoOM · Apr 02, 2015 at 09:23 PM

     function Update()
          {
            if(dealers >=1)
             {
                if(methScript.meth >=1)
                  {
                     while(true)
                      {
                       InvokeRepeating("Deal",0,3);
                      }
                  }
             } 
          }
 
 function Deal()
 {
    moneyScript.money +=20;
    methScript.meth -=1;
 }

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 GH0STfdd · Apr 03, 2015 at 06:20 PM 0
Share

With this script unity crash :(

avatar image Bunny83 · Apr 03, 2015 at 06:48 PM 0
Share

@UNZoO$$anonymous$$: You have the same infinity loop which never exits and isn't part of a coroutine (contains at least 1 yield statement) as the OP. So you're trapped inside the while loop "forever".

avatar image UNZoOM · Apr 03, 2015 at 07:05 PM 0
Share

ah i realize , my bad. Sorry for wrong guidance.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Nested Array? Populating a menu using a loop. 0 Answers

Using ForEach Loop to change variables on prefabs 1 Answer

WaitForSeconds is not waiting for seconds, at all. Its like it doesnt exist. 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