- Home /
JS to C# convert problem
here is the JS version:
var energyPoints:float = 100.0;
function MakeDamage (amount:float){ energyPoints -= amount; }
function Update () { if(energyPoints <= 0){ Destroy(transform.root); } }
and here is the C# version:
using UnityEngine; using System.Collections;
public class damageControll : MonoBehaviour {
public float energyPoints = 100.0f;
void MakeDamage(float amount)
{
energyPoints -= amount;
}
void Update() { if (energyPoints <= 0) { Destroy(transform.root); } } }
I am using SendMessage("MakeDamage",damage) from other script. The JS version work just fine. C# version don't work. And by converting the JS script with http://m2h.nl/files/js_to_c.php I get the same code that I wrote by converting it by hand. I can't understand the error i make here. (and I a did a lot of research,and i can't solve this one. by my look such a simple looking problem).XD
Thank you in advance.
P.S. Look like I can't access $$anonymous$$akeDamage in C# version, and I did test the same script with public void $$anonymous$$akeDamage(){}.
Answer by beloto · Dec 02, 2010 at 05:22 PM
I found that you can't have the same name of the script in C# and JS. If you have 2 scripts with the same name, unity do not recognize the C# version as component. So line like:
if ((collision.gameObject.GetComponent("damageControll") != null) && (collision.transform.root.tag == attackThisTag))
{
collision.gameObject.SendMessage("MakeDamage",damage);
}
the GetComponent("damageControll") part will return null even if you have attached damageControll.cs on the object, and the body of the if will not execute.
The problem was not in the converting from JS to C#.
Answer by duck · Oct 30, 2010 at 06:51 PM
Make sure the script that is trying to call the MakeDamage function is actually sending a float, and not an int.
JS might be flexible in that regard, but C# will likely ignore it because the type of the function's argument doesn't match.
Oh, also the method must be declared public. Functions in JS are public by default, but in c# the default is private, so change the declaration line to:
public void MakeDamage(){
public float damage = 5.0f; yup it is float. or there is a different type of float? Thank you b.t.w. :P
Yeah that is the strange part. I did comment that part in the question.I did test the same script with public in declaration line .... Hmmmm .... I newer restarted the scene. Let me check that again :P
Nope .... public don't help. restart don't help also. XD this is retarded. ill just use the JS for that script. Thank you Duck on the help. :P
Your answer
![](https://koobas.hobune.stream/wayback/20220613081406im_/https://answers.unity.com/themes/thub/images/avi.jpg)