- Home /
How to hit game object in coroutine
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.
In this tutorial, he used coroutine http://www.youtube.com/watch?v=oFzT17$$anonymous$$1Xxs for spawned objects, how can I hit spawned object in cocoutines.
Answer by hd27 · Dec 09, 2014 at 03:27 PM
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
}
I'll try that. Why you did not use while(true) ins$$anonymous$$d of calls itself coroutine for making loop?
I can't hit when I use coroutine where am I missing? Codes are below
Answer by abalta · Dec 09, 2014 at 10:59 PM
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;
}
Try WaitForEndOfFrame(), ins$$anonymous$$d of waiting for 2 to seconds.
Your answer
Follow this Question
Related Questions
Raycast smooth rotation and distance 0 Answers
how can I add the delay between different function calls in C#.. 3 Answers
Script Crashing 1 Answer
Cutscene Segments 1 Answer