Any ideas why this might lag on iPhone 4?

hello

I have this piece of code that lerps UI Text’s font size up and down to achieve a pulsating effect. If I do this by animating the scale the entire rect transform comes down while growing and it just doesn’t look as i need it to, and I’d rather have a direct control on pulses than change animation every time anyway.

bool hsDoOnce = true
bool hsGrow = true

    	void Update()
    	{
    		if(hsReached)
    			NewHighscore();
    	}
    
    
    	public void NewHighscore()
    	{
    		if(hsDoOnce)
    		{
    			SoundPlayer.get.PlaySound (SoundName.HighScore);
    			hsLerpTime = 0;
    			hsDoOnce = false;
    		}
    
    		if(hsGrow)
    		{
    			levelCounter.fontSize = Mathf.CeilToInt(Mathf.Lerp(100, hsFinalSize, (hsLerpTime += Time.deltaTime)* 9));
    			if(levelCounter.fontSize >= hsFinalSize)
    			{
    				hsLerpTime = 0;
    				hsGrow = false;
    			}
    		}
    		else
    		{
    			levelCounter.fontSize = Mathf.FloorToInt(Mathf.Lerp(hsFinalSize, 100, (hsLerpTime += Time.deltaTime)* 9));
    			if(levelCounter.fontSize <= 100)
    			{
    				hsPulseCounter--;
    				hsLerpTime = 0;
    				hsGrow = true;
    				if(hsPulseCounter <= 0)
    					hsReached = false;
    			}
    		}
    	}

This is fine in the engine but causes a lag spike on iPhone 4. Any ideas why may this code be so heavy?

You are changing the Text every frame I believe, If you put a timer on it so it pulses every couple seconds it would be much, much more efficient on ram.

Changing the font size every frame will cause a new texture atlas for the font to be regenerated every frame, which is something to avoid. Look for alternative ways to scale your high score message. Since the text isn’t changing, you could either use an image, or generate an image with the text at runtime.