- Home /
Switch code from Js to C#
i found this code:
var switch : Boolean;
var oldSwitch : Boolean;
function Update () {
if(switch && switch == !oldSwitch) {do stuff; oldSwitch = switch;}
}
But i need it in C# could someone help me. Thanks in advance!
Answer by Dave-Carlile · Apr 29, 2013 at 03:10 PM
The syntax between the two is fairly similar. It shouldn't be hard for you to do some searching on C# to see the syntax for what you want to do.
There isn't much to do for your example though...
public bool switch;
public bool oldSwitch;
void Update()
{
if (switch && switch != oldSwitch)
{
do stuff
oldSwitch = switch;
}
}
Look at what the differences are, and you should be able to apply this to much of your other code that needs converted.
I guess the variables should be public (which is the default in UnityScript). Also i wouldn't name a variable "switch", since that's a language keyword.
Good points, @Bunny83. Switch is a language construct in Javascript too, so it's not the best choice there either. I guess in C# it may not even compile - might want to change it to something else regardless - something that indicates what the switch is for.
Thanks a lot! i got confused by the word switch, i used other names for it now. If anyone is wondering what the is used for, i found it and it works great for doing something only when the bool is just activated.
Your answer
Follow this Question
Related Questions
switching textures on mouseUp (C#) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Stop Player movement when bool changes 2D 3 Answers
List of List of Booleans function giving weird results 1 Answer