Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abalta · Dec 09, 2014 at 02:43 PM · coroutinehit

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Dec 09, 2014 at 02:52 PM 0
Share

Coroutines does not "have more performance".

avatar image abalta · Dec 09, 2014 at 02:56 PM 0
Share

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.

avatar image EvilTak · Dec 09, 2014 at 03:04 PM 0
Share

By hit you mean mouse click? Or collision?

avatar image abalta · Dec 09, 2014 at 03:15 PM 0
Share

mouse click :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abalta · Dec 09, 2014 at 03:31 PM 0
Share

I'll try that. Why you did not use while(true) ins$$anonymous$$d of calls itself coroutine for making loop?

avatar image EvilTak · Dec 09, 2014 at 03:59 PM 0
Share

Yeah while(true) is better.

avatar image abalta · Dec 09, 2014 at 10:57 PM 0
Share

I can't hit when I use coroutine where am I missing? Codes are below

avatar image
0

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;            
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hd27 · Dec 10, 2014 at 11:19 AM 0
Share

Try WaitForEndOfFrame(), ins$$anonymous$$d of waiting for 2 to seconds.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Coroutines and Lerp - how to make them friends? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges