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 Inan-Evin · Feb 26, 2015 at 07:45 PM · coroutinestringmethodstartcoroutinestopcoroutine

Is there any performance differences between Coroutine methods ?

Hello everyone, I want to ask a real simple question. I am basically writing a script for zoom in & out according to the mouse input. I could just put all the code into the Update and lerp the object through boolean checks, but the problem is I need to be extra careful about script optimization in this project. So I avoid using update a lot, which means I use coroutines in order to lerp the object for zooming in & out. My problem is, I want the user to be able to have quick responses on mouse click. For example, right click starts zooming, but if I click it again in the middle of zooming in, I want it to zoom out, I don't want it to wait for the first coroutine to finish zooming. So I need to use StopCoroutine. For example :

     if(Input.GetButtonDown("Fire2"))
     {
     
     pressedZoomButton = !pressedZoomButton;
     
     if(pressedZoomButton)
     {
     StopCoroutine("ZoomOut");
     StartCoroutine("ZoomIn");
     }
     
     else{
     StopCoroutine("ZoomIn");
     StartCoroutine("ZoomOut");
     
     }
 }

It works fine, no problems, but at first I tried to do it by using the method name, then I learned in order the StopCoroutine function to work it needs to be used with string method, also it needs to be used on a method which is also called with string method. I did it, it works, but I got a little bit suspicious, is using these coroutine string methods costy ? Will it cause any performance problems, because as far as I know strings are the things that we need to approach carefully.

Also really what is the difference between calling it by method name and by string, I mean technically ?

Comment
Add comment · Show 1
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 fafase · Feb 27, 2015 at 09:36 AM 0
Share

Historically, the call with string allowed to stop the coroutine so that would have been the reason to use it. Now that the latest updates include the StopCoroutine(IEnumerator), the use of string may become obsolete pretty soon.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by BenouKat · Feb 26, 2015 at 08:00 PM

If you check the documentation : http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

It specifies that the Coroutine with a string gets a higher overhead runtime when you start it.

Despite this, no, there's not really a technical difference. It's just 2 ways to manage them, one is more costly at start.

Comment
Add comment · 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
2

Answer by DanSuperGP · Feb 26, 2015 at 08:20 PM

It's worth noting that, in Unity 5 you can store a reference to a running coroutine and stop it using the reference rather than a string. This will be more efficient than using strings.

This code throws an error in 4.6 and works perfectly in Unity 5

 using UnityEngine;
 using System.Collections;
 
 public class CoroutineTest : MonoBehaviour {
 
     public bool doOnce;
 
     public float TimeFromStart = 5;
 
     Coroutine coLoop;
 
     // Use this for initialization
     void Start () {
 
         coLoop = StartCoroutine(CoroutineLoop());
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Time.time > TimeFromStart && ! doOnce)
         {
             Debug.Log ("StoppingCoroutine");
             StopCoroutine(coLoop);
 
             doOnce = true;
         }
 
     
     }
 
     IEnumerator CoroutineLoop()
     {
         while(true)
         {
             yield return null;
 
             Debug.Log("Still Running");
 
 
         }
 
     }
 
 }
Comment
Add comment · Show 4 · 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 BenouKat · Feb 26, 2015 at 08:36 PM 1
Share

Didn't know it (didn't check Unity 5 yet).

Good they finally did it.

avatar image steakpinball · Feb 26, 2015 at 09:38 PM 1
Share

This feature is in 4.6

avatar image DanSuperGP · Feb 26, 2015 at 09:44 PM 0
Share

Hmm.. It was throwing an error for me in 4.6.2 but not in 5...

avatar image BenouKat · Feb 27, 2015 at 07:53 AM 0
Share

I confirm it works in 4.6.1.

Thanks !

avatar image
0

Answer by tomhendriks1102 · Sep 28, 2017 at 03:35 PM

might not totaly be what you need in this case, but you could use a number or boolean to change with mouseclicks in your update and have that coroutine check the value to know if it should zoom in/out or not zoom. this way u dont have to start/stop coroutines and just have one running all the time basically "listening" for a new command to zoom again.

Comment
Add comment · 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

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

22 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

Related Questions

Coroutine won't stop running. Tried String. 1 Answer

StopCoroutine not returning to yield 0 Answers

Stop a coroutine on a single gameobject 1 Answer

StopCoroutine() - how to release memory to the garbage collector? 1 Answer

How to force Coroutine to finish 1 Answer


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