Raining Coins

hi,

I’m making this App for IOS, which needs raining coins in it. No i’ve created a script which instantiates coins over an amount of time and spawns them on a certain area:

#pragma strict

var rangeSpawnFromCentre : int;
var updateCounter : float;
var fallingCoins : float;
var coinsInArea : int;
var coinCounter : int;
var prefabCoin : Transform;
var Coins : GameObject;
var Coin : GameObject;
var force : int;


function Update () 
{
	if(PlayerPrefs.GetString("Found") == "true"  coinCounter <= 10)
	{
		updateCounter -=Time.deltaTime;
		if(updateCounter < 0.0)
		{
			updateCounter = fallingCoins;
			spawnCoin();
		}
	}
}

function spawnCoin()
{
	coinSpawner(Vector3(this.transform.position.x + Random.Range(-rangeSpawnFromCentre, 
	rangeSpawnFromCentre), this.transform.position.y, 
	this.transform.position.z + Random.Range(-rangeSpawnFromCentre, rangeSpawnFromCentre)));
}

function coinSpawner(position : Vector3)
{
	Instantiate(Coin, position, Quaternion.identity);
	Coins.transform.Rotate(Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180));
	Coins.rigidbody.AddForce(Vector3.down * force, ForceMode.Impulse);
	coinCounter++;
	
}

But whenever I instantiate a prefab of a Coin, the game freezes a milisecond. I’ve read about this problem on the forum, so i think instantiating a prefab isn’t the right option for IOS. So what’s the next best alternative to get a coin rain working?

I had and still get this problem on IOS all the time. My problem seemed to be a non mobile shader in the prefab causing a big hit on creation. Sometimes it’s a sound that causes it. My game TorpedoRun suffers from it on the 1st time an explosion is called. BUT all further explosions don’t have the delay. very weird.

All I can say is that pooling is a much better approach than instantiate for mobile. Just pre-create a pool of coins, turn them off, coinX.active = false; then when you need them, make them active. I actually just, for the most part, turn off colliders and renderers. But basically, the lack of instantiate, and then the inevitable use of Destroy() is far less costly to the engine.

Definitely pool - look out for PoolManager.