- Home /
The question is answered, right answer was accepted
Help Trying to translate cs to js problem with one block of code
this is the script im trying to translate i changes some words around to reference the scripts im using
using UnityEngine; using System.Collections;
public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float cooldown;
// Use this for initialization
void Start () {
attackTimer = 0;
cooldown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer =0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer ==0) {
Attack();
attackTimer = cooldown;
}
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2.5f) {
if(direction >0) {
EnemyHealth eh = (EnemyHealth)target . GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
} this is how far i got
public var target:GameObject;
public var attackTimer:float;
public var cooldown:float;
// Use this for initialization
function Start () {
attackTimer = 0;
cooldown = 2.0f;
}
// Update is called once per frame
function Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer =0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer ==0) {
Attack();
attackTimer = cooldown;
}
}
}
private function Attack() {
var distance : float = Vector3.Distance(target.transform.position, transform.position);
var dir:Vector3 = (target.transform.position - transform.position).normalized;
var direction : float = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2.5f) {
if(direction >0) {
var PlayerHealthbarph :PlayerHealthbar ;
PlayerHealthbarph = (PlayerHealthbar)target.GetComponent("PlayerHealthbar");
ph.AddjustCurrentHealth(-10);
}
}
}
the problem seem to be in here where i get the error
var PlayerHealthbarph :PlayerHealthbar ;
PlayerHealthbarph = (PlayerHealthbar)target.GetComponent("PlayerHealthbar");
ph.AddjustCurrentHealth(-10);
}
}
}
this is the cs version
EnemyHealth eh = (EnemyHealth)target . GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
the error i get says Assets/Scripts/PlayerAttack.js(47,53): UCE0001: ';' expected. Insert a semicolon at the end.
i really new to scripting and its probably so simple error also does anyone know a good place to learn js srcipting i know the basics but i never found a place that go's deep into learning how to script
Your brackets and not all of your code are in code blocks, along with things being like
}
}
}
}
It's very hard to read.
ok will try to fix brackets the code not on block i tried to fix but the first part would not go on the blocks that was the only way it would post thanks for the advice
Answer by rasheedqw · Jul 27, 2013 at 06:18 PM
both the scripts were in java the problem was the i wrote the reference as PlayerHealthbar when i should have put Playerhealthbar thank for all the help it working now
Answer by aldonaletto · Jul 27, 2013 at 04:57 PM
GetComponent("PlayerHealthbar") returns a Component - that's why it's coerced to the right type. In JS you may typecast the result with the as keyword, like this:
PlayerHealthbarph = target.GetComponent("PlayerHealthbar") as PlayerHealthbar;
But the string version of GetComponent should be avoided - it's faster and easier to specify the type directly, since GetComponent returns the correct type:
PlayerHealthbarph = target.GetComponent(PlayerHealthbar);
i tried PlayerHealthbarph = target.GetComponent(PlayerHealthbar);
when i first did it but kept getting this error Assets/Dragonforged/dragonforged/playerAttack.js(40,31): BCE0018: The name 'PlayerHealthbar' does not denote a valid type ('not found'). tried it again got the same error i know i spelled PlayerHealthbar the same and i have the script not sure what the problem is
Now the problem probably is the incompatibility C#-JS: their compilers don't talk to each other, thus the JS compiler can't see C# scripts (and vice-versa). You can either convert PlayerHealthbar to JS, what would make it visible at compile time, or use the compilation order to make sure PlayerHealthbar.cs has been compiled in a previous stage - once compiled, the scripts can be seen by everybody, no matter which was the original language. In order to use the compilation order, place the JS scripts in a custom Assets folder (Assets/JsScripts, for instance) and let the C# scripts in some Standard Assets subfolder - they will be compiled first, beco$$anonymous$$g visible to any scripts. Take a look at this topic in the docs to know more about compilation order.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# into JavaScript 1 Answer
can someone translate this to java I've tried a thousand time but couldn't do it 1 Answer
Can someone help me translate this to c#? 1 Answer
Unity get component help 1 Answer