• 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 /
avatar image
1
Question by qua1ity · Oct 15, 2014 at 08:17 PM · convert

Help with converting c# to JS

Hello! I'm trying to write a shot script for my character. I'm following the tutorial on Pixelnest on how to do this, on the following section http://pixelnest.io/tutorials/2d-game-unity/shooting-1/

All my code is in JavaScript, and the tutorial is written in c# and there is one part that i just can't get right. It's in the last part of the WeaponScript on Pixelnest. It looks like this:

   /// <summary>
   /// Is the weapon ready to create a new projectile?
   /// </summary>
   public bool CanAttack
   {
     get
     {
       return shootCooldown <= 0f;
     }
   }
 }


How would that part look like in JavaScript? Not sure on how to use "get". Would very much appreciate if someone could help me with this. My full JavaScript version of WeaponScript looks like this (without the last part which i need help with):

 #pragma strict
 
 var projectile : Transform;
 var canAttack : boolean;
 var shootingRate : float;
 var shootCooldown : float;
 
 function Start () {
     shootCooldown = 0f;
 }
 
 function Update () {
     if (shootCooldown > 0) {
         shootCooldown -= Time.deltaTime;
     }
 }
 
 function Attack (isEnemy : boolean) {
     if (canAttack) {
          shootCooldown = shootingRate;
          var shotTransform : Transform;
          shotTransform = Instantiate(projectile);
          
          // Assign position
          shotTransform.position = transform.position;
          
          // The is enemy property
          var shot : ShotScript = shotTransform.gameObject.GetComponent(ShotScript);
          
          if (shot != null) {
              shot.isEnemyShot = isEnemy;
          }
     }
 }
Comment
Add comment · Show 2
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 KpjComp · Oct 15, 2014 at 08:27 PM 0
Share

function get CanAttack { return shootCooldown

avatar image qua1ity · Oct 16, 2014 at 07:26 PM 0
Share

Tried that, but it didn't work. $$anonymous$$y script looks like this:

 #pragma strict
 
 var projectile : Transform;
 var canAttack : boolean;
 var shootingRate : float;
 private var shootCooldown : float;
 
 function Start () {
     shootCooldown = 0f;
 }
 
 function Update () {
     if (shootCooldown > 0) {
         shootCooldown -= Time.deltaTime;
     }
 }
 
 function Attack (isEnemy : boolean) {
     if (canAttack) {
          shootCooldown = shootingRate;
          var shotTransform : Transform;
          shotTransform = Instantiate(projectile);
          
          // Assign position
          shotTransform.position = transform.position;
          
          // The is enemy property
          var shot : ShotScript = shotTransform.gameObject.GetComponent(ShotScript);
          
          if (shot != null) {
              shot.isEnemyShot = isEnemy;
          }
     }
 }
 function get CanAttack () { 
     return shootCooldown <= 0f; 
 }


The script runs, but the last cooldown part doesn't work. Any ideas on how to make this work with the cooldown?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by pako · Oct 16, 2014 at 09:15 PM

function get CanAttack () { return shootCooldown

However, you should also not declare the canAttack variable, and use if(CanAttack) inside the Attack function.

Comment
Add comment · Show 2 · 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 Bunny83 · Oct 16, 2014 at 09:54 PM 1
Share

You're missing the "get" keyword for actually turn the method into a property. If you declare it as "normal" method you have to use the calling brackets () when using it. Other than that you're right, remove "canAttack" and replace it with this property / method call

avatar image pako · Oct 16, 2014 at 10:13 PM 0
Share

Thanks Bunny83, I corrected it!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The Unity move - converting application from C++ to C# 3 Answers

software that makes scripts for you 6 Answers

Help...convert C# to Js ??? 3 Answers

Exported SWF no longer loads 1 Answer

can someone help me to convert this from pc to mobile platform 0 Answers


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