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!

I like the "do stuff" part :)

1 Answer

1

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 <a href="http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.80).aspx">language keyword</a>.

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.