Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Oct 22, 2013 at 10:03 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by rasheedqw · Jul 27, 2013 at 04:16 PM · c#javascriptcomponenttranslate

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

Comment
Add comment · Show 4
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 tw1st3d · Jul 27, 2013 at 04:24 PM 0
Share

Please format this properly so we can help you better.

avatar image rasheedqw · Jul 27, 2013 at 05:29 PM 0
Share

i get that ever question what does that mean

avatar image tw1st3d · Jul 27, 2013 at 06:20 PM 0
Share

Your brackets and not all of your code are in code blocks, along with things being like

 }
 }
 }
 }

It's very hard to read.

avatar image rasheedqw · Jul 27, 2013 at 06:31 PM 0
Share

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · 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
1

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);
Comment
Add comment · Show 3 · 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 rasheedqw · Jul 27, 2013 at 05:40 PM 0
Share

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

avatar image rasheedqw · Jul 27, 2013 at 05:46 PM 0
Share

sorry had the h lower case in the Playerhealthbar

avatar image aldonaletto · Jul 27, 2013 at 06:00 PM 0
Share

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

Answers Answers and Comments

17 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

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


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