Hi, I want to hit game object in coroutines with mousedown function. When I hitted gameobject, it will be destroyed. I used update() function for this, but I realized coroutine has more performance.
You can use the coroutine just like you will use the Update() loop;
C# example:
Ienumerator SomeCoroutine()
{
if(Input.GetMouseButtonDown(0)) // If left mouse is down
{
//Do Something
}
yield return null //Waits 1 frame
StartCoroutine("SomeCoroutine") //The coroutine calls itself
//so it will loop
}
IEnumerator HitObject ()
{
while (true) {
yield return new WaitForSeconds (2.0f);
spawnNext ();
//mouse ile tıklama, telefonlarda dokunmaya denk geliyor.
if (Input.GetMouseButtonDown (0)) {
if (Global.globalInstance.pauseFlag) {
hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
}
if (hit.collider != null) {
if (hit.collider.tag != "Untagged") {
if (hit.collider.tag == "num101") {
playAudio (0);
Global.globalInstance.myScore += 101;
Global.globalInstance.myScoreText.text = Global.globalInstance.myScore.ToString ();
Global.globalInstance.countPicked101 += 1;
Destroy (GameObject.FindGameObjectWithTag (hit.collider.tag));
CheckHighscore ();
} else {
Global.globalInstance.grabScore.Add (hit.collider.tag);
updateScore (Global.globalInstance.grabScore.Count);
Global.globalInstance.myScoreText.text = Global.globalInstance.myScore.ToString ();
Destroy (GameObject.FindGameObjectWithTag (hit.collider.tag));
}
}
}
}
}
}
void spawnNext ()
{
// Random Index
Global.globalInstance.whichNumber = Random.Range (0, generateWeighedNumber.Count);
Global.globalInstance.whichPosition = Random.Range (0, Global.globalInstance.spawnerPoints.Length);
// Spawn Number at current Position
Vector2 startPos = new Vector2 (Global.globalInstance.spawnerPoints [Global.globalInstance.whichPosition].transform.position.x, Global.globalInstance.spawnerPoints [Global.globalInstance.whichPosition].transform.position.y);
Global.globalInstance.spawnedGameobject = (GameObject)Instantiate (generateWeighedNumber [Global.globalInstance.whichNumber], startPos, Quaternion.identity);
Color color = new Color (Random.Range (0.5f, 1f), Random.Range (0.5f, 1f), Random.Range (0.5f, 1f), 1);
Global.globalInstance.spawnedGameobject.GetComponent<SpriteRenderer> ().color = color;
if (Global.globalInstance.Timer > randomTimer) {
Global.globalInstance.Timer = 0f;
randomGravitySpeed += 0.1f;
Global.globalInstance.secondsBetweenSpawn = Random.Range (0.7f, 1.0f);
randomTimer = Random.Range (30, 60);
if (randomGravitySpeed >= 2.5f) {
randomGravitySpeed = 2.5f;
}
}
Global.globalInstance.spawnedGameobject.GetComponent<Rigidbody2D> ().gravityScale = randomGravitySpeed;
}