Translating JS to C#

Hi, I translated a js script to c# that made a enemy in my game have a death animation. I went it unity and there were no compiler errors but when I played my game and killed the enemy it just stayed still and played the idle animation.
Here’s my script:

using System.Collections;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    int EnemyHealth = 10;
    GameObject TheZombie;

    void DeductPoints(int DamageAmount)
    {
        EnemyHealth -= DamageAmount;
    }

    void Update()
    {
        if (EnemyHealth <= 0)
        {
            GetComponent<ZombieFollow>().enabled = false;
            TheZombie.GetComponent<Animation>().Play("Dying");
            EndZombie();
        }
    }

    IEnumerator EndZombie()
    {
        yield return new WaitForSeconds(3);
        Destroy(gameObject);
    }
}

and here is the JS version:

var EnemyHealth : int = 10;
var TheZombie : GameObject;

function DeductPoints (DamageAmount : int) {
    EnemyHealth -= DamageAmount;
}

function Update () {
    if (EnemyHealth <= 0) {
        this.GetComponent("ZombieFollow").enabled=false;
        TheZombie.GetComponent.<Animation>().Play("Dying");
        EndZombie();
    }
}

function EndZombie () {
    yield WaitForSeconds(3);
    Destroy(gameObject);
}

I don’t know how Unity handled JS, but it looks like you want to use EndZombie as a Coroutine?
In C# you would have to start a Coroutine with:
StartCoroutine(EndZombie ());

Maybe the JS version handled these in a different way and it wasn’t require to call StartCoroutine there?

Also why do you need to check if EnemyHealth <= 0 in Update instead of just checking it after you changed the value in DeductPoints? Currently the code inside the if runs every frame as soon as the EnemyHealth is <= 0, which means you would also Play the “Dying” animation every frame, so you might need to move this if inside DeductPoints or you need a additional flag that you can check in the if condition for example “IsDying” or something like that.

1 Like

right on the mark, i’m surprised unity doesn’t tell you about the coroutine not being called properly.

no need for the update method as he said, just a waste of cpu.

So it would be like this?

using System.Collections;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
    int EnemyHealth = 10;
    GameObject TheZombie;
    StartCoroutine(EndZombie ()):
    void DeductPoints(int DamageAmount)
    {
        EnemyHealth -= DamageAmount;
    }
    void Update()
    {
        if (EnemyHealth <= 0)
        {
            GetComponent<ZombieFollow>().enabled = false;
            TheZombie.GetComponent<Animation>().Play("Dying");
            EndZombie();
        }
    }
    IEnumerator EndZombie()
    {
        yield return new WaitForSeconds(3);
        Destroy(gameObject);
    }
}

It might be better to skip anything written in old java/unityscript. For one thing, we mostly stopped using it several versions ago. So most are probably doing things in an obsolete way. Also, as RP1Fake points out, that original code has some sloppy parts. Javascript was often written by beginners for other beginners.

1 Like