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 /
avatar image
0
Question by Cello · Jul 15, 2012 at 04:27 AM · c#variablesyntax

Variable References in C# from JS

Hello,

I'm currently watching some scripting tutorials which involve scripting for a 2D game in Javascript, though i'm using C#. So far I've had no problem translating it, but when it comes to anything related to variables i'm pretty confused since C# has a different syntax for those.

I've been able to figure out most, but when I have to refer to other functions in the variable I have no clue how to go about doing it. Here's what i'm currently translating:

 var controller : CharacterController = GetComponent (CharacterController);
 private var moveDirection : Vector2 = Vector2.zero;

I've tried my best to troubleshoot it with the console myself, but the best I could come up with is this (which is basically the same thing, only it doesn't work, ha ha):

 private var moveDirection = Vector2 = Vector2.zero;
 public var controller = CharacterController = GetComponent (CharacterController);

Which then returns these errors within the console:

 Assets/Scripts/Player/PlayerAnimation.cs(62,17): error CS0825: The contextual keyword `var' may only appear within a local variable declaration
 Assets/Scripts/Player/PlayerAnimation.cs(63,16): error CS0825: The contextual keyword `var' may only appear within a local variable declaration

I've tried declaring them as local variables, public variables, and public strings but it usually makes the problem worse. Any help in translating would be appreciated, and an explanation to help me understand the syntax variables/references in C# would be amazing, but it's not really required.

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by delstrega · Jul 15, 2012 at 05:03 AM

 var controller : CharacterController;

in UnityScript equals

 CharacterController controller;

in C#.

The ": something" just tells the compiler explicitly what type to expect since UnityScript can figure that out on it's own normally, it's optional there.

So

 var controller : CharacterController = GetComponent (CharacterController);
 private var moveDirection : Vector2 = Vector2.zero;

just becomes

 CharacterController controller = GetComponent(CharacterController);
 private Vector2 moveDirection = Vector2.zero;

although you might also write

 CharacterController controller = GetComponent<CharacterController>();


Hope that helps.

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 Cello · Jul 15, 2012 at 06:06 AM 0
Share

$$anonymous$$nowing what order everything should go definitely was the biggest problem I had with this, but I understand it perfectly now. Thank you very much! It feels good to finally get back to making progress.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trying to call a variable from another script in C# 1 Answer

Can't access a javascript static variable from c# script 1 Answer

Variables not setting correctly 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