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 /
avatar image
0
Question by DavidDebnar · Jul 11, 2011 at 10:22 AM · variablesdeclaration

Rewriting C# to JavaScript

Hey! I'm rewriting a C# script to JavaScript, so I can better understand it, because I'm bad at C#. I ran into few problems. First. Where to place the variables, that are declared between functions? Than this.

    public WorldData WorldData
    {
        get { return m_WorldData; }
    }

It's at the start, at declaring. How can I write it in JS?

  • David

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

2 Replies

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

Answer by CHPedersen · Jul 11, 2011 at 10:26 AM

That particular piece of code is a getter for a Property called WorldData. Properties are a C# language feature, they're not available in JS. Your only option in JS that doesn't break other scripts that might reference WorldData is to create a public variable called WorldData instead.

By the way, your question title needs to be reversed. You're asking about C# to JS. ;)

Comment
Add comment · Show 7 · 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 DavidDebnar · Jul 11, 2011 at 11:15 AM 0
Share

Thanks, and what about if (Physics.Raycast(ray, out hit, 4.0f)) ? what is the OUT?

avatar image Aldwoni_legacy · Jul 11, 2011 at 11:26 AM 3
Share

As far as I know you can use it without the "out".It is only needed in C#

avatar image CHPedersen · Jul 11, 2011 at 11:29 AM 3
Share

The "out" keyword specifies that the parameter, hit, is passed by reference to Physics.Raycast, and that it is the responsibility of Physics.Raycast to set the variable in its definition. Another method that uses this keyword in .Net is the TryParse-method of float, int and double.

See the documention on Physics.Raycast for JavaScript usage: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

avatar image DavidDebnar · Jul 11, 2011 at 11:30 AM 0
Share

thanks, ill try :) $$anonymous$$+ ;)

avatar image Peter G · Jul 11, 2011 at 12:40 PM 2
Share

You can actually do properties in js. See my answer.

Show more comments
avatar image
4

Answer by Peter G · Jul 11, 2011 at 12:39 PM

You can actually do properties in js. The syntax is funny, but doable. First you need to explicitly declare the class, then the getter:

 public class MyClass extends MonoBehaviour {
     private var myVar : int = 3;
 
     function get MyVar : int { 
          return myVar;
     }
     function set MyVar (value : int) {
         myVar = value;
     }
 }

then it is accessible as

 myInstance.MyVar

While the syntax looks really strange to any C# dev, I do like the fact that there's a function in there to remind you properties are methods.

BTW, info taken from this question.

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 · Jul 11, 2011 at 02:36 PM 0
Share

Didn't knew that it's possible in US. I don't use JS but i would always use properties. Changing a property into a public variable is a design break. Of course is you recompile all related code it will work, but only in this special case. If you do other things in the getter / setter it can't be done with a public var ;) Just think of a singleton instance.

+1

avatar image DavidDebnar · Jul 12, 2011 at 08:55 AM 0
Share

Thanks.. But I managed to not rewrite it, because it was time consu$$anonymous$$g, and i can read C# so maybe another time... And it's really good that there is get/set in JavaScript :).

$$anonymous$$+!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Which variable can be implicitly declared? 1 Answer

GameObject.Find either can't be called or is in the wrong scope. 0 Answers

javascript: declaring a variable in 'while' loop 2 Answers

best place to declare variables in the script 2 Answers

Problem when trying to save variable in another variable. 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