- Home /
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!
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.
$$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
Follow this Question
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