Optimizacion: Coroutines and Pausing the game

Hi guys from Spain!!

2 boys and me (we are trying to start an indie studio: ALE Games Studio :slight_smile: ) are doing a 2D game for mobiles and tablets. And we just find a very big problem: Optimizacion!!!

We notice that we have problems with 2 points:

-Function to pause the game, below the function we use. And when this funcion is running, suddendly, frames per second decreases a lot!! and Main Thread increases. Why?

public void Pausar()
	{
		if(boolsGlobal.pause)
		{
			boolsGlobal.pause=false;
			Time.timeScale=1.0f;
		}
		else
		{
			boolsGlobal.pause=true;
			Time.timeScale=0f;
		}
	}

The other point: The Courutines. When we use them, the performance decreases. Why? For example, this script consumes many resources:

IEnumerator Start () {
		/*if (pointA == null) {
			Vector3 pointA = transform.localPosition;
		}*/
		while (true) {
			yield return StartCoroutine(MoveObject(transform, pointA, pointB, tiempo));
			yield return StartCoroutine(MoveObject(transform, pointB, pointA, tiempo));
		}
	}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
	{
		var i= 0.0f;
		var rate= 1.0f/time;
		while (i < 1.0f) {
			i += Time.deltaTime * rate;
			thisTransform.localPosition = Vector3.Lerp(startPos, endPos, i);
			yield return null; 
		}
	}

Thank you!!!

while (true) {
yield return StartCoroutine(MoveObject(transform, pointA, pointB, tiempo));
yield return StartCoroutine(MoveObject(transform, pointB, pointA, tiempo));
}

You are starting coroutines in a coroutine with a while loop. I am guessing that is getting out of hand really fast.
Also, I would make PAUSAR a simple switch. There is no need to check boolean every frame.

Thank you for answering!! Looking at the scrips I found another thing that is wrong. This script decreases the optimizacion. I use it in the enemies, they look for the character and if the character is in a certain range, they follow him in order to attack him. I put this in Update.

RaycastHit2D hit = Physics2D.Raycast (transform.position, objetivo.position);
	Vector2 direccion = new Vector2 ((objetivo.position.x - transform.position.x), (objetivo.position.y - transform.position.y));
	//velhit = velIzquierda;
	if (((objetivo.position.x > xizquierda)  (objetivo.position.x < xderecha)))
	{
		anim.SetBool("atacar",false);
		anim.SetFloat ("Speed", Mathf.Abs (velhit));
		
		//rigidbody2D.velocity = new Vector2 (-(transform.position.x - objetivo.position.x) * 2, 0);
		if(objetivo.position.x-transform.position.x < 0) // el protagonista esta a la derecha del gusano
		{
			if(this.transform.localScale.x < 0)
				Flip ();
			if(direccion.x > 0.2f)
			{
				rigidbody2D.velocity = new Vector2 (0, 0);
				velhit = 0;
			}
			else{
				rigidbody2D.velocity = new Vector2 (-8, 0);
				velhit = -8;
			}
			if(direccion.magnitude < 5)
			{
				Debug.Log ("se para");
				rigidbody2D.velocity = new Vector2 (0, 0);
				velhit = 0;
				if(!atacarr)
				{
					anim.SetBool("atacar",true);
					atacarr=true;
					objetivo.GetComponent<Vida>().eliminarVida(1);
					StartCoroutine("Example");
				}
				
			}
			
			
		}
		else if (objetivo.position.x-transform.position.x > 0)
		{
			if(this.transform.localScale.x > 0)
				Flip ();
			if(direccion.x < 0.2f)
			{
				rigidbody2D.velocity = new Vector2 (0, 0);
				velhit = 0;
			}
			else{
				rigidbody2D.velocity = new Vector2 (8, 0);
				velhit=8;
			}
			if(direccion.magnitude < 5)
			{
				Debug.Log ("se para");
				rigidbody2D.velocity = new Vector2 (0, 0);
				velhit = 0;
				if(!atacarr)
				{
					anim.SetBool("atacar",true);
					atacarr=true;
					objetivo.GetComponent<Vida>().eliminarVida(1);
					StartCoroutine("Example");
				}
			}
		}

you know how you do to get that bolded text?
well do instead.

Sorry!! I put it as a code :wink: