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 /
avatar image
0
Question by Cero-San · May 18, 2017 at 11:10 AM · unity 5scripting problemupdatelogicframe

How to prevent Unity to execute all the code in the first frame.

So, i'm creating a card game with Unity3D and im stuck in a logic issue: in Start() i call a function called SinglePlayer(); and the function is the core of the game.

Singleplayer() has inside it another function NextTurn(); that is called to decide who shall play first on the next turn.

When the AI starts first it does its turn instantly and this is good, but when it's my time to play a card the code doesn't wait for me to Drag and Drop a card into the table, but tries to get instantly my card from the table and fails.

There's a way to pause the code from executing? I tried with infinite loops but Unity crashes, i tried with CoRoutine but it doesn't work. There's anything else i should try?

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 SohailBukhari · May 18, 2017 at 11:41 AM 1
Share

There are many ways to pause the code as you can make bool and check in update whether you have complete your turn or not. You can make unity event and when you complete your turn he invoke event and do something with the AI. As in your case you can use timer and wait for time to elapsed then take decision.You can make callbacks function for executing code.

you can use WaitUnitil, it Suspends the coroutine execution until the supplied delegate evaluates to true. WaitUntil can only be used with a yield statement in coroutines.

avatar image Cero-San SohailBukhari · May 18, 2017 at 03:34 PM 0
Share

I tried WaitUntil(); and It doesn't work. I'll try with writing a bit of my program, maybe it'll be more clear.

 public class SinglePlayer : $$anonymous$$onoBehaviour {
 
 private void Start()
     }
         GiocatoreSingolo();    //aka singleplayer in italian lang.
     }
 
     private void Update()
     {
         targetTime -= Time.deltaTime;
 
         if (CountCards(table) == 2)
         {
            2CardsOnTable = true;
         }
 
         if (CountCards(table) == 1)
         {
             1CardOnTable = true;
         }
     }
 
 private void GiocatoreSingolo()
 {
          if($$anonymous$$yTurn==true)
          {
                StartCoroutine( try1(1CardOnTable,targetTime));
 
                 if(1CardOnTable==true)  anotherFunction();
 
          }
 
         else
               somethingElse();
 
 }
 
 public IEnumerator try1(bool Cards, float timer)
     {
         yield return new WaitUntil(()=>Cards==true || timer ==0);
     }
 
 }

The problem is that the CoRoutine doesn't work and is skipped so AnotherFunction() is called even if the time, or the bool are not right.

avatar image NoseKills Cero-San · May 18, 2017 at 04:19 PM 0
Share

You are still approaching this from the wrong perspective.

You shouldn't try to "stop" the execution of code from progressing to the next line (you can, with an infinite loop, but as you noticed, it's​ not what you want).

If you completely block the execution from progressing in a single threaded program, ALL of the code in the program will stop executing until you unblock, including the code implemented by Unity that reads keyboard input and redraws the screen etc. etc. That's why Unity "crashes" (the right word is "hangs" since it doesn't exit to desktop) It is "supposed" to do that.

What you want to do is "not do anything in Update()" when you are waiting. This can be as simple as

 void Update() {
     if (waitingForPlayer$$anonymous$$ove) return;

Coroutine can't be used to make the rest of the app pause. Yielding means exactly that the coroutine "yields" and gives the rest of the app a chance to do all it wants. You could use coroutines and yielding to delay execution of code that's inside the coroutine until a condition is met, but why do that when you can use the same condition in an IF in Update() to prevent execution of the delayed code.

As a side note, timer==0 will pretty much never be true. Let's say you start from timer=2f... Time.deltatime is the time between screen redraws and depends on your apps performance It's something like 0.027153f for example. It's​ extremely unlikely that a bunch of frame times combined would amount to exactly 2f. The condition should be timer<=0. Actually you should never trust a floating point number to be exactly something. You can google "floating point precision" to read more.

Show more comments
avatar image toddisarockstar · May 19, 2017 at 01:45 AM 0
Share

what you are asking is super simple. Use an if statement! function update continuosly reads its code every frame. and is the perfect place to check if conditions are met.

 void Update () {  
  If(playerjustdragged==true) {
 playerjustdragged=false;
 
      //this will execute once everytime the bool becomes true
 
  }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Fanttum · May 18, 2017 at 03:55 PM

 void Update () {  

 If(doNotExcute) {    
 // code
 }
 else {    
 // code    
 }
 
 }
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to trigger udpate in editor mode only when I modify component parameters 1 Answer

Game over funcition isn't being called accurately? 2 Answers

Animating GUI Texture - Help 4 Answers

FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer

How can I change ApiCompatibilityLevel before Unity start? 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