Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by seandolan · Mar 28, 2016 at 12:44 AM · classconvertc# to javascript

Unsure of how to convert c# public class to javascript

I am working on a tutorial series and recoding everything in Javascript from C#. I am only doing this as the tutorial is perfect for me but not in Javascript. I know enough C# to read some of the lines of code, but not enough to write it from scratch. This particular part makes objects "Living Entities" and allows them to take damage if hit by a projectile.

I have a script called IDamageable.js and so far it only contains the following:

 function TakeHit(damage:float,hit:RaycastHit){
 
 }

In C# a script called LivingEntity.cs uses the following lines to somehow connect the IDamageable part:

 public class LivingEntity : MonoBehaviour, IDamageable {
 
 }

I am unsure of what the comma does. Like the IDamageable part in that line. So I am unable to ask the question directly of how to implement an [Extend? Value? Whatever?] in Javascript. So my question is, what is the IDamageable parameter called.. and how do I do the same thing in Javascript. My apologies in advance, I think my question could be a lot more straight forward if I knew the terminology of the position IDamageable occupies in the C# code.

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 seandolan · Mar 28, 2016 at 04:06 AM 0
Share

The the IDamageable part an interface? It looks similar to other interfaces when I kept searching around. If someone can identify this I am sure I could find the resources I need to progress already on the internet.

avatar image Fredex8 · Mar 28, 2016 at 07:09 AM 1
Share

It means it is inheriting from the IDamageable class. Variables and functions in that class are also accessible to LivingEntity.

This should show you the equivalent code in JS.

1 Reply

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

Answer by taxvi · Mar 28, 2016 at 07:55 AM

It's common practice in coding to name interfaces ISomething so most probably IDamageable is an interface (look up the difference between class and interface if you don't know already). Any class can have only 1 parent class (`MonoBehavioiur` in your case) but it can also implement infinite number of interfaces.

Interface is a collection of method headers without body. When implementing an interface in a class you have to write body for all the methods (or it will not compile). Using interface is good for casting different objects in same method, or iterating through them.

For example, in your case you may have an explosion in your scene so you'd write something like:

 foreach(IDamagable d in ExplosionZone){
     d.TakeHit(10f);
 }

So in this way you can take any class implementing IDamageable like Tiger and DeathStar and cast them in this loop and they will both take hit even though they don't have a common parent class.

So, for now, in your LivingEntity class you have to implement all the methods that are declared in IDamageable to let your stuff compile.

Comment
Add comment · Show 1 · 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 seandolan · Mar 28, 2016 at 08:00 AM 0
Share

Thank you. This is enough for me to find out how to achieve this in JavaScript.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference Vector 3 variable to float variable. 2 Answers

Heightmap From Texture - Script Converter 2 Answers

Copy and Pasted code for Trajectory and got one parsing error... 1 Answer

Can't instantiate new class C# 0 Answers

List of Class With GameObject 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