- Home /
Converting variable from javascript to C#
Hi,
I just started learning Unity and teaching myself C#. I'm following an online tutorial that uses JS, and I'm trying to convert the code to C#.
I've used the m2h.nl jsToC# converter site, and this is the error I'm getting:
Javascript:
private var instanBoosters;
C#:
private FIXME_VAR_TYPE instanBoosters;
I've tried assigning the var as a string or float, but I get the errors.
Any idea what can replace the variable type on that line? Thanks.
Answer by kag359six · Nov 29, 2012 at 02:33 AM
OK, so it depends on what you are instantiating the object as. Usually you would define it as transform. So it would be:
Transform instanBoosters;
//when you instantiate do this:
instanBoosters= Instantiate(object,position,rotation) as Transform;
Thanks for the reply. I'll look more into this. I think it boils down to a level of familiarity with each language and the Unity particulars. It still doesn't properly work, but at least it's a direction I can explore more in depth.
Answer by kag359six · Nov 28, 2012 at 11:05 PM
You have to know what the Type of object instanBoosters is, which is the issue with the dynamic nature of javascript. In javascript you don't have to define the type unless there is no value in it. In C# you can't just randomly switch the type. For example, you can't make an integer a float unless you cast. If it's a string you would do:
String someString; //define type, then name of variable.
Do you know what type of object instanBoosters is?
Exactly, just take a look at the script and find out where the variable is used. That's the only way to figure out what type it should have.
Another recommendation is to take seperate lessons in C# as a language rather than for Unity itself. It helps understand it better.
Your answer
Follow this Question
Related Questions
Help with conversion from javascript to c# 3 Answers
Multiple Cars not working 1 Answer
Trying to convert this javascript code to C# 1 Answer
C# to Java Script Conversion 2 Answers
Distribute terrain in zones 3 Answers