Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MiguelCurwen · Feb 06, 2020 at 07:40 PM · androidcoroutinesingletonlambda

Lambdas, Singleton and Coroutine behaves different on Unity play and Android

Hi, I found myself struggling a lot to understand what is really happening with the following:

Let's say I have a spawner and some player abilities. All the abilities are working fine, when I use them, a lambda callback is called with the active effect like this:

 private Action<PlayerHandler> HealPlayerAction()
     {
         return (p) => p.HealPlayer((int)value);
     }

Everything fine here.

But the following is happening: When I call an ability to force enemies to the ground, just changing a boolean, it all works nicely on Unity pressing play, but when I compile and upload it to Google Play to test it, the behaviour is totally different:

     class AbilitiesHandler
     {
      ...
      private Action<PlayerHandler> NoFlyingEnemiesAction()
         {
 
             //I don't use the p parameter but it should remain there because of the Action expected
             return (p) => EnemySpawnerManager.Instance.ForceAllEnemiesOnGround(true);
 
         }
      ...
      }
 
 
 
     class EnemySpawnerManager (is a singleton)
      {
      ...
      public void ForceAllEnemiesOnGround(bool enabled)
         {
             _forceGround = enabled;
         }
      ...
      
      public void Spawn()
         {
      ...
              if (!_forceGround)
             {
                 enemy.Flying = newEnemy.Flying;
             } else
             {
                 enemy.Flying = false;
             }
      ...
      }
 
 
 }



So, I placed a lot of Logs, and everything is being called right, the ability when pressed... the method on EnemySpawner.... BUT, the change on _forceGround is never saw in the Spawn() method, that everytime that checks _forceGround is false.

I can't debug the code with breakpoints on android because of authentication, and I would really like to understand what might be happening there... are the lambdas working differently on Android? Is the combination between a Singleton and the lambda? First time I face a problem like that, is something so simple as a bool change.

Thanks for the help!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MiguelCurwen · Feb 06, 2020 at 09:18 PM

Okay, I tried a lot of different options here, what seems to be very important and I didn't include before is that the Spawn() method of EnemySpawner, is called from a Coroutine placed inside other script "EnemyWaves".


If I try to change anything inside EnemySpawner from the ability or player scripts, those changes are not seen inside the said coroutine using Spawn. I have tried:

  • Changing private bool from method.

  • Changing public bool from the other script with the Instance reference.

  • Changing a public static bool.

All those 3 options were always false, so no change being seen.


The twist is, if I make changes from the EnemyWaves, owner of the coroutine calling the Spawn() inside EnemySpawner, just before calling the method Spawn(), the bools are changed, so trying to put a boolean to true produced the next results:

  • Static bool from coroutine owner looked inside the Spawn script: true

  • public bool inside the Spawn script, changed from coroutine owner before calling: true

  • Static bool inside Spawner script changed from anywhere but coroutine owner: false

  • Anything accesing the Spawn script from anywhere but coroutine owner: false


My conclusion here is that on Android, coroutines or singleton (or the addition of both) work differently than running on Unity, and the values of the Spawner script were encapsulated in memory inside the coroutine, making every other change produced outside from coroutine invisible.


Maybe I'm wrong on this assumption, but is the only logical deduction I can make right now. I would be happy to hear anyone's oppinion!

Thanks for reading!

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

255 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 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 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 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 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 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 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 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 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 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

Errors and bugs in player using unity ads. Works fine in editor. 0 Answers

Coroutine Changed local variable behaviour in lambda, some confused 0 Answers

Singletons with coroutines? 4 Answers

DontDestroyOnLoad duplicate object when using a singleton 5 Answers

WaitForSeconds(3) on Android waits less than expected 2 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