- Home /
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
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);
}
}
}
Thanks for solution!
I have 2 colliders on Player and trigger touch both of them. So simple yet so confusing!