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 Ghopper21 · Oct 13, 2012 at 03:30 PM · buildbugcoroutine

Odd behavior changing another script's variable via coroutine

Following a Unity tutorial (the coconut shy minigame in Will Gladstone's Unity 3.x Game Development Essentials), I've created a game in which there are three targets. These targets stay "down" for 3 seconds after being hit. If you hit all three targets before any come back up, you win.

The code, boiled down to its essence, is:

 // in a script attached to each target
 
 public class TargetCollision : MonoBehaviour {
     
     void OnCollisionEnter(Collision col) {
         StartCoroutine("targetHit");
     }
     
     IEnumerator targetHit() {
         WinDetector.targets++;
         yield return new WaitForSeconds(3.0f);
         WinDetector.targets--;
     }
 }
 
 // in a script attached to a separate GameObject
 
 public class WinDetector : MonoBehaviour {
 
     public static int targets = 0;
     
     void Update () {
         Debug.Log(String.Format("targets = {0}", targets));
         if (targets == 3) {
             Debug.Log("You win!");
         }
     }
 }
 

When run from within Unity, the game works in the sense that reaches 3 and the win is counted. However, something strange happens: after reaching 3, targets gets decremented down to -3, not back to 0 as intended (which can be seen from Debug.Log output).

When run from a Mac standalone non-dev build, the win condition never gets triggered, as targets never reaches 3. In this build, targets also ends up getting decremented down to -3.

When run for a Mac standalone dev build, things start to fall apart, in the sense of getting the Mac pinwheel and eventual hanging of the application after I hit a few targets.

My guess is that I'm doing something wrong with the coroutine and there is some kind of race error in the way targets is incremented, decremented, and read.

What's going on?

Comment
Add comment · Show 6
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 dannyskim · Oct 13, 2012 at 04:42 PM 0
Share

How are you detecting collisions on the target? It could also be that even when your targets are down, they're still checking collisions and firing off the coroutine.

avatar image Ghopper21 · Oct 13, 2012 at 06:24 PM 0
Share

@dannyskim -- I'm using the OnTargetCollision method above to detect collisions. In the actual code, this checks to see if the target is already down, in which case it doesn't increment/decrement WinDetector.targets again. But in any case, why would targets ever go negative? Why would its behavior be different when running within Unity versus in the builds?

avatar image whydoidoit · Oct 13, 2012 at 09:09 PM 0
Share

Something else must be decrementing targets - I can think of a way it would get higher and not decrement, but not lower.

avatar image Ghopper21 · Oct 14, 2012 at 12:28 AM 0
Share

@whydoidoit -- I'm not familiar with how to debug Unity scripts -- is there a way to watch a variable for change? If so, I could see if there's some other script messing things up. Also, any guess why the behavior would be different running within Unity versus in a build?

avatar image whydoidoit · Oct 14, 2012 at 12:35 AM 0
Share

I'm not sure you can set a "watch point" might be easier to continually log the value and then specifically log where you think you are adding and removing it. I'm guessing (because these things usually are bugs in the code not Unity) that the difference between build and editor is in the ti$$anonymous$$g of some other script which is affecting this value. But it's certainly odd.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Distribute terrain in zones 3 Answers

Mouse cursor is giant in Mac builds. 1 Answer

Unity bugged after Ios Build 0 Answers

Set texture / EnableKeyword not working in build 1 Answer

FixedUpdate has stopped working 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