Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ykuser1012 · Dec 08, 2018 at 06:03 PM · variablecoroutinemember variables

Difference of between a member variable and a instant variable in a coroutine

 class Class1 : MonoBehaviour  
 {  
   
 ...  
   
 float var1;  
   
 ...  
   
 IEnumerator Coroutine1()  
 {  
     var1 = 0f;  
     float var2 = 0f;  
     float t = Time.unscaledTime;  
     while(var1 < 100f)  
     {  
         var1 += (Time.unscaledTime - t);  
         var2 += (Time.unscaledTime - t);  
         Debug.Log("var1 : " + var1 + " var2 : " + var2);  
         yield return new WaitForSeconds(Time.fixedDeltaTime);  
         t = Time.unscaledTime;  
     }  
 }  
   
 ...  
   
 }  

  

In this situation, The values of var1 and var2 are different.
I'm giving the same operation to var1 and var2, so why are there different values?

(Sorry for my bad English)

Comment
Add comment · Show 2
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 Bonfire-Boy · Dec 08, 2018 at 06:33 PM 1
Share

Is there something else changing the value of var1 perhaps?

By the way, waiting for fixedDeltaTime in a coroutine (which gets updates at deltaTime intervals) seems rather odd to me. What are you trying to achieve there?

avatar image ykuser1012 Bonfire-Boy · Dec 08, 2018 at 07:13 PM 0
Share

There's only one difference which is whether the variable is member variable or instant variable.

I tried to make a function where the value was not affected by the timeScale and was decreasing regularly.

(I'm a beginner at Unity, so I don't know if this is a good idea.)

This code is only an excerpt which is relevant to my question.

(I'm sorry for my poor English once again.)

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by nicholasw1816 · Dec 08, 2018 at 07:33 PM

The member variable is stored, when the Coroutine updates, it will (- time) from it's previous value the last time it was updated. The instance value will refresh back to 0 or (null) each Coroutine update.

Comment
Add comment · Show 2 · 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 ykuser1012 · Dec 08, 2018 at 07:47 PM 0
Share

Well... I solved this problem.

It was very foolish mistake. The coroutine has been called several times because of a hole in my code.

Anyway, thank you for your answer.

avatar image Bonfire-Boy · Dec 08, 2018 at 08:07 PM 0
Share

This answer is wrong, it seems to be suggesting that every time the coroutine gets an update, it goes back to the start of the function (which would defeat the whole point of a coroutine). In fact, both variables are set to zero at the same point, outside the loop.

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

110 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 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 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 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

How to get a variable out of a coroutine? 3 Answers

How Return Or Restart A Coroutine When A Variable Increase? 0 Answers

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

How Return Or Restart A Coroutine When A Variable Increase? 1 Answer

Pass delegate to coroutine as a variable 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