• 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
2
Question by beloto · Oct 30, 2010 at 05:44 PM · convertsendmessage

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.

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 beloto · Oct 30, 2010 at 05:49 PM 0
Share

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(){}.

avatar image cregox · Feb 28, 2011 at 04:07 PM 0
Share

related: http://answers.unity3d.com/questions/5507/what-are-the-syntax-differences-in-c-and-javascript

2 Replies

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

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#.

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 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(){ 
Comment
Add comment · Show 4 · 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 beloto · Oct 30, 2010 at 07:08 PM 0
Share

public float damage = 5.0f; yup it is float. or there is a different type of float? Thank you b.t.w. :P

avatar image duck ♦♦ · Oct 30, 2010 at 08:04 PM 0
Share

See edit - the method needs to be made public

avatar image beloto · Oct 31, 2010 at 05:40 PM 0
Share

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

avatar image beloto · Oct 31, 2010 at 05:54 PM 0
Share

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

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

No one has followed this question yet.

Related Questions

Converting pc mouse input to iphone joysticks? 1 Answer

Convert GameObject to String? 2 Answers

Convert to C#,I don't know how to convert this 1 Answer

Covert to C# from java script 3 Answers

Using variables from other script and converting them to another variable 2 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