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 Torqus · Sep 16, 2018 at 09:03 AM · errorvariableloopfor-loopfor

x=x Assignment made to same variable

I have this weird problem where I have a for inside of a coroutine, and I want to set the classic "int i=0" to be a variable that's passed to the coroutine, called "time", but the syntax is wrong so I have to say "time=time", it works but it says "hey dummy you are saying time is equal time".

     IEnumerator RunTimer(GameObject obj, float time)
     {
         obj.GetComponent<Timer>().TimerStart.Invoke();
         Debug.Log(obj.name);
         for (time = time; time > 0; time -= Time.deltaTime)
         {
             currentTime = time;
             yield return null;
         }
         TimerEnded();
     }

I can fix it by making a new "float timer" and change it to "time = timer", which basically also does nothing.
So is this just a dumb unity alert or is there a correct way of doing this?

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

3 Replies

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

Answer by StevenUnu · Sep 16, 2018 at 03:22 PM

There is another easier way than transforming the for loop into a while loop. You see, you can omit the initializing in the for syntax. C# won't complain.

 for(; time > 0; time -= Time.deltaTime)
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 Bunny83 · Sep 16, 2018 at 04:42 PM 0
Share

This isn't really new^^. It always has been like that. Only the condition ( middle part ) is required. The first and third part is optional.

avatar image StevenUnu Bunny83 · Sep 16, 2018 at 04:46 PM 1
Share

omg, I wrote new? I meant "another" ins$$anonymous$$d of "a new". $$anonymous$$y $$anonymous$$d plays tricks on me

avatar image
2

Answer by SkaredCreations · Sep 16, 2018 at 09:29 AM

It's just a warning that you're writing a "strange" assignment because assigning itself to a variable does not make sense as it means nothing essentially, so the compiler is wondering if you meant something else. You can ignore this warning or just use a new variable in the "for" statements.

Or also convert the "for" loop into a "while" logic (best choice imo), for example:

 while (time > 0f)
 {
     currentTime = time;
     yield return null;
     time -= Time.deltaTime;
 }

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
0

Answer by Torqus · Sep 17, 2018 at 03:36 AM

Both answers work well, thanks. I actually ended "using" my problem because it helps me get the current time for other objects that need it.

         for (currentTime = time; time > 0; time -= Time.deltaTime)
         {
             currentTime = time;
             yield return null;
         }

So the While example could have been useful too.

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

124 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 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 have a (wait) yield on a for loop? 0 Answers

How do I reset a for loop variable??? 3 Answers

Help With "For Loop" Not Working? 2 Answers

Using for ... var ... in loop to access variable. 1 Answer

Having Trouble with this For Loop 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