Hello Guest

Author Topic: Crash on set color on iOS  (Read 3533 times)

etoreo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Crash on set color on iOS
« on: September 19, 2012, 06:22:19 am »
I have this code to change the color of a white sprite that works in the Unity debug player and Windows player:
Code: [Select]
   void redrawTank()
   {
      if(mTankConfiguration == null)
         return;
     
      TankScript pTankScript = mTank.GetComponent<TankScript>();
      if(pTankScript == null)
         return;
      pTankScript.Configureation = mTankConfiguration;
     
      float percentSpeed = (pTankScript.Configureation.getSpeed() - TankConfigureation.MIN_SPEED) / (TankConfigureation.TOP_SPEED - TankConfigureation.MIN_SPEED);
      if(SpeedSprite == null || percentSpeed > 1.0f || percentSpeed < 0.0f)
         return;
     
      tk2dSprite pSprite = SpeedSprite.GetComponent<tk2dSprite>();
      if(pSprite == null)
         return;
     
      Color pColor = pSprite.color;
      pColor.b = 0.0f;
      pColor.g = percentSpeed;
      pColor.r = 1.0f - percentSpeed;
      pSprite.color = pColor;
   }

On the very last line of that function, it crashes on iOS devices.  I have tried everything I can think of and I do nearly the exact same thing in other places in my code, but this is the only place it crashes and I can't figure out how to make it play nice on iOS.  The stack trace is given in the image attached.

Any thoughts?

etoreo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Crash on set color on iOS
« Reply #1 on: September 19, 2012, 07:55:50 am »
I have a work around that will be ok for me - I don't allow the color to be set when the program wakes up.  I wait till the sprite will be shown on the screen (it's a menu system) to color it.  The bug is still there on Awake, but I have worked around it.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Crash on set color on iOS
« Reply #2 on: September 19, 2012, 09:59:10 am »
I think know why this is happening.

The sprite initialization happens in Awake() - this is where all the buffers are allocated.

If your function is called from Awake, it may get called before the sprite is actually initialized, and thus setting color will be writing into null buffers.

The order of Awake functions being called is underined in Unity. It might do it one way on PC and another on iOS.

Things to try -

You can try the script execution order in Unity and put tk2dSprite before your script, hopefully this'll fix it.

Alternatively, try switching where redrawTank gets called to Start() - there should be no difference in functionality, but it should get called AFTER the sprites are awake.

Hope that help!