Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_NgpsXlcGwdegUw · Jul 22, 2020 at 09:46 AM · coroutinedebugwhile-loop

My code is working wierd! Please help me understand why!,My code is working weird! Pleas help me understand why

Hey here is the code:

int in editor is equal 10

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class deadlyPlant : MonoBehaviour {

 public int damagetime;


 public void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player")
     {

         StartCoroutine(StartTakingDamage());

     }
 
 }
 IEnumerator StartTakingDamage()
 {
     while (damagetime > 0)
     {
         yield return new WaitForSeconds(4f);
         damagetime --;
         Debug.Log(damagetime);
         
     }
     
 }

}

As far as I remember decrementation decrease variable by 1, but Debug in editor shows that every secound int was decreased by 1 twice. Cen some one tell me why? Maybe there is some thing wrong with loop? Please help me understand why it`s working like that.

Thx,Hey, here is th code

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class deadlyPlant : MonoBehaviour {

 public int damagetime;


 public void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player")
     {

         StartCoroutine(StartTakingDamage());

     }
 
 }
 IEnumerator StartTakingDamage()
 {
     while (damagetime >= 0)
     {
         yield return new WaitForSeconds(4f);
         damagetime --;
         Debug.Log(damagetime);
         
     }
     
 }

}

Int = 10 in editor

As far as I know decrementation decrese variable by 1 but Debug every 4 secounds decrease number by 1 twice. It`s probably something with my code. I`m beginner so can someone help me understand why it`s working that way?

[1]: /storage/temp/163848-pytanko.png

pytanko.png (6.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by smark12007 · Jul 22, 2020 at 05:57 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class deadlyPlant : MonoBehaviour {
 
     public int damagetime;
     public void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "Player") {
             Debug.Log(other, other); // Add this line to see what happen.
             StartCoroutine(StartTakingDamage());
         }
 
     }
     IEnumerator StartTakingDamage() {
         while (damagetime >= 0) {
             yield return new WaitForSeconds(4f);
             damagetime--;
             Debug.Log(damagetime);
 
         }
 
     }
 }
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 unity_NgpsXlcGwdegUw · Jul 22, 2020 at 07:24 PM 1
Share

Thanks for solution!

I have 2 colliders on Player and trigger touch both of them. So simple yet so confusing!

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

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

When there is a while-loop in a Coroutine, if I have a "yield" within the while-loop, does that mean the Coroutine will be re-entered within the while loop? 1 Answer

Understanding yield inside a for or a while loop 2 Answers

Index out of range in while statement but I can't figure out how. 1 Answer

While loop VS WaitWhile in coroutines 3 Answers

Coroutine "While" Setup 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