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 LeftyTwoGuns · Sep 08, 2014 at 01:50 AM · coroutineboolean

Can you check a boolean in a coroutine?

I have one object checking for a coroutine in Update to return true, in which case it will detach itself from its parent.

But to me, it seems like a waste to be checking every single frame, as I don't expect the boolean to change that frequently. Is it possible to check in a coroutine? Or would it even matter? Is this even a noticeable impact in overhead?

My specific script is pretty simple:

 public EnemyDeath enemyDeath;
 
 void Start () {
 
         enemyDeath = GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyDeath>();
 
 }
 
 void Update(){
 
         if(enemyDeath.enemyDead){
 
             transform.parent = null;
         }
 
Comment
Add comment · Show 7
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 kacyesp · Sep 08, 2014 at 02:00 AM 1
Share

Checking a single boolean will not break or make your frame rate. It should be okay to check it in Update().

avatar image Addyarb · Sep 08, 2014 at 02:12 AM 1
Share

Can you post your script, or an altered version of it to show what you mean?

avatar image AyAMrau · Sep 08, 2014 at 02:17 AM 0
Share

From your description it sounds a bit like this could be handled by a delegate, ins$$anonymous$$d of the constant checking. But had to tell without details.

avatar image LeftyTwoGuns · Sep 08, 2014 at 02:25 AM 0
Share

Updated with the script

avatar image kacyesp · Sep 08, 2014 at 02:26 AM 0
Share

Oh if that's all you're doing then go ahead and check in the coroutine and set the parent to null in the coroutine.

Show more comments

2 Replies

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

Answer by AyAMrau · Sep 08, 2014 at 02:53 AM

Instead of constant checking you could use a delegate.

In the EnemyDeath script add:

 public System.Action OnDeath;

And then in the same script, at the point where you set the enemyDead to true add:

 If(OnDeath != null) // make sure something is assigned
 {
    OnDeath();
 }


Then in the script you shown in start:

  GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyDeath>().OnDeath += OnEnemyDeath;

And in the same script as a method:

 private void OnEnemyDeath()
 {
    transform.parent = null;
    GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyDeath>().OnDeath -= OnEnemyDeath;
 }

This is a bit more typing, but gives you the knowledge about state change without constant checks, while still not forcing the EnemyDeath to know about other scripts.

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
2

Answer by Kiwasi · Sep 08, 2014 at 04:39 AM

One way to do this is properties.

 private bool _enemyDead;
 public bool enemyDead {
     get {
         return _enemyDead;
     }
     set {
         // Take action here
         _enemyDead = value;
     }
 }
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 Kiwasi · Sep 08, 2014 at 04:39 AM 0
Share

Some thoughts on properties:

Using properties will add a method call to checking the bool, so it will be more expensive in terms of performance each time. But it will be cheaper then checking every frame.

Properties also tend to be set and forget. There is no code required to maintain them every frame. This works great if you trigger a property from a coroutine. Also useful for data validation.

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

26 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

Related Questions

make events occur only at specific times 1 Answer

I have chains of connectable objects which reference eachother. Need them to pass a simple boolean to eachother! How? 1 Answer

Delay something until no object has a certain condition 2 Answers

Coroutines not waiting for yield waitforseconds to finish? 1 Answer

Generic Cooldown IEnumerator? 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