Why do my first few collectable/powerup collisions cause lag but not later ones?

Hello, I’m having an issue with my iphone game where when the player object, (which uses a character controller) walks into a collectable/powerup it causes a very slight lag spike.

However, and here comes the weird part, this only seems to happen the first four times or so, after which the collectables behave very smoothly, I’d like to know why this is happening.

Heres the script I’m using:

#pragma strict 

var supplyValue:int = 1;
internal var parentRigidbody: Rigidbody;
internal var parentTransform: Transform;

function Awake ()
{
	parentRigidbody = this.transform.parent.rigidbody;
	parentTransform = this.transform.parent.transform;
}

function OnSpawned () 
{
	parentRigidbody.isKinematic = false;
	countdownTillCleanup();  
	countdownTillPhysicsOff();
	parentRigidbody.AddForce(-Vector3.up * 400);  
}

function countdownTillCleanup()
{
	yield WaitForSeconds(30);
	PoolManager.Pools["Consumables"].Despawn(parentTransform);
}

function countdownTillPhysicsOff()
{
	yield WaitForSeconds(3); 
	parentRigidbody.isKinematic = true;
}

function OnTriggerEnter(col : Collider)
{
	if (col.tag == "player")
	{ 
		col.SendMessageUpwards("IncreaseSupplies", supplyValue, SendMessageOptions.DontRequireReceiver);
		PoolManager.Pools["Consumables"].Despawn(parentTransform);
	}

}

The script uses poolmanager 4 to instance the collectables, twenty of which are pre spawned at the start of play, so this lag can’t be due to collectables being initiated or destroyed.

It may also be worth noting that the collectable gameobject consists of a mesh render, a rigidbody and a box collider. Then a child gameobject consisting of a much larger trigger box collider and the above script.

The player object meanwhile consists of a character controller, listener, then a bunch of unrelated scripts and numerous child objects none of which have the player tag or colliders of their own.

To clarify the lag spikes only occur on the iphone when the player object collides with a collectable, and only the first four or so times such collisions occur.

I’ve been trying all kinds of things for some time to try and resolve this without any luck and I’m really hoping one of you has encountered something like this before, i’d be very much grateful for any responses though.

Sending messages tends to be highly inefficient, especially for iOS. How many times is this called, the first time?

A little update on this, i've manage to track the source of the lag down to the actual act of displaying the supplies variable (which the send message changes) within the GUI, as removing it from the onGUI removes the lag associated with running into a collectable. However I still haven't been able to figure out why displaying this string based number causes lag when its updated, whilst a practically identically functioning countdown variable I'm displaying with the GUI doesn't cause lag.

1 Answer

1

SOLVED IT! Alright, for the sake of anyone with a similar problem who stumbles upon this later, the lag spikes for the first few collisions were the fault of me displaying the variable being changed as a result of the collision using a font with Character set to dynamic. This seemingly meant that the first time it showed any one character it needed a moment to create a texture for it on the fly, thusly the first few times my player object picked up a collectable it had to create the textures for numbers 1 - 9, which in the case of a few of them, resulted in lag spikes. Changing the font character to ASCII has resolved the problem since all textures for fonts for that variable displaying GUI are now generated beforehand.

Converted your comment to an answer (as it is one!) Good to know that - thanks....

I've been having this issue with my current game as well. Setting it to ASCII removed the darn sudden fps drop which is critical for gameplay. Thanks alot!