Convert JS to C# (621372)

I need some help to get a script from JS to C#. I have some problems getting it rewritten.

Can i get some help to rewrite this code?

var PowerLevel : float = 10000;
var LightPowerUse : float = 1;
var ChargeSpeed : float = 0.5;
var InstantCharge : int = 3;

//Private variables:
    private var flashlightOn : boolean = false;
    private var SpotAngleMax : float = 80;
    private var LightLevel2 : float = 0f;
    private var LightLevel3 : int = 0;
    private var MaximumPowerLevel : float = PowerLevel;
    private var AngleMultiplier : float = 1;
    private var NormalAngle : float = SpotAngleMax;
    private var ShowAngle : int = 0;
    private var PowerUsage : float = 0;
    private var PowerCharge : float = 0;
    private var PowerUse : boolean = false;

function Update ()
{
    PowerUsage = 0;
    PowerCharge = 0;
    PowerUse = false;

    //Light max spot angle
    if(SpotAngleMax > 179)
    {
        SpotAngleMax = 179;
        print("Spot and show angle can max be 179 degrees");
    }

    //Enable the flashlight
    if(Input.GetAxis("Fire1"))
    {
        LightLevel2 = PowerLevel / 10000;
        GetComponent.<Light>().intensity = LightLevel2;
        //GetComponent.<Light>().spotAngle = LightLevel2 * SpotAngleMax;
        GetComponent.<Light>().spotAngle = SpotAngleMax * AngleMultiplier;
        PowerLevel -= LightPowerUse * AngleMultiplier;
        PowerUsage += LightPowerUse * AngleMultiplier;
        PowerUse = true;
        flashlightOn = true;
        if(flashlightOn == true && Input.GetAxis("Fire2"))
            AngleMultiplier = 0.1300007;
    } else {
        GetComponent.<Light>().intensity = 0;
        flashlightOn = false;
        AngleMultiplier = 1;
    }

    //print("Light percent: " + intens /100 + "%");

    if(PowerLevel < 0)
        PowerLevel = 0;

    if(PowerLevel > MaximumPowerLevel)
        PowerLevel = MaximumPowerLevel;

    //Charging the battery
    if(PowerUse == false && PowerLevel < MaximumPowerLevel)
    {
        PowerLevel += ChargeSpeed + PowerUsage;
        PowerCharge += ChargeSpeed;
        PowerUsage -= ChargeSpeed;
    }

    LightLevel3 = PowerLevel / 100;

    //Instant charge the battery
    if(Input.GetKeyDown(KeyCode.X))
    {
        if(InstantCharge > 0)
        {
            InstantCharge -= 1;
            PowerLevel = MaximumPowerLevel;
            print("Instant charge used");
        } else {
            print("No more charges");
        }
    }

    //Scroll wheel
    if(Input.GetAxis("Mouse ScrollWheel") > 0)
    {
        if(AngleMultiplier < 2.229999 && flashlightOn == true)
        {
            AngleMultiplier += 0.01;
        }
        //print("Scroll up");
    } else if(Input.GetAxis("Mouse ScrollWheel") < 0 && flashlightOn == true)
    {
        if(AngleMultiplier > 0.1300007)
        {
            AngleMultiplier -= 0.01;
        }
        //print("Scroll down");
    }

    //Resetting the angle multiplier when pressing down scroll wheel
    if(Input.GetAxis("Fire3"))
    {
        if(flashlightOn == true)
            AngleMultiplier = 1;
    }

    ShowAngle = SpotAngleMax * AngleMultiplier;
}

function Reset()
{
    PowerLevel = 10000;
    InstantCharge = 3;
    flashlightOn = false;
}

function OnGUI()
{
    //Show the power level
     GUI.Box(Rect(100, 0, 100, 20),"Power: " + LightLevel3 + "%");

    //Show instant charges
     if (InstantCharge > 0)
     {
            GUI.Box(Rect(100, 20, 100, 20),"Instant: " + InstantCharge);
     } else {
         GUI.Box(Rect(100, 20, 100, 20),"no charges");
     }
 
    //Show flashlight angle
     if(Input.GetAxis("Fire1"))
     {
         if(ShowAngle == 80)
         {
             GUI.Box(Rect(0, 40, 100, 20),"Angle: def");
         } else if(ShowAngle == 179)
         {
             GUI.Box(Rect(0, 40, 100, 20),"Angle: max");
         } else if(ShowAngle == 10)
         {
             GUI.Box(Rect(0, 40, 100, 20),"Angle: min");
         } else {
             GUI.Box(Rect(0, 40, 100, 20),"Angle: " + ShowAngle);
         }
     } else {
         GUI.Box(Rect(0, 40, 100, 20),"Flash off");
     }

    //Show power usage
     if(PowerUsage > 0)
     {
         GUI.Box(Rect(100, 40, 100, 20),"Usage: " + PowerUsage + "%/s");
     } else if(PowerLevel == MaximumPowerLevel)
     {
         GUI.Box(Rect(100, 40, 100, 20),"Max Charge");
     } else {
         GUI.Box(Rect(100, 40, 100, 20),"Charge: " + PowerCharge + "%/s");
     }
}

And what are the problems?
Post what you’ve got so far.

i scrapped the code i wrote and began researching. it is easier for me if someone could help me to transform this into C#. Then i would be able to write my own C# codes. this is mostly my most advanced code i have written in JS

I don’t understand how you came to the conclusion that someone else rewriting your code will enable you to be capable of it. Especially when you cannot even perform a basic Google search. First result for “UnityScript to C#” is an automatic conversion tool.

http://www.m2h.nl/files/js_to_c.php

Everything you see in the script i posted i did learn from other’s script. used 2 days to write it. most of the time to come up with ideas what to add

EDIT: i have tryed that tool. it didnt work that well

It wouldn’t take much time to translate the script. But that doesn’t make much sense.

There are about 6 differences that you need to find out about for this translation.
Have a look here. Especially #4. That less-than-2minutes-video already covers 4 differences that you need here.

2 more differences:

  • The generic method call is almost the same, just remove the dot in front of the angle brackets.
  • the assigment of a class member cannot be another class member (sometimes also referred to as instance variable), it has to be a static class variable or a constant value / constant (const) variable.

And that’s all if i didn’t miss anything.

2 Likes

i have tried to watch videos about the differences and then rewrite the code but the code wouldn’t run. The code didn’t compile at all. i always got errors

The video I’ve linked explains 4 differences in direct comparison. 2 minutes and you can’t do much wrong.
The other both differences that I’ve added are done in less than 10 seconds, because it’s simply about removing a dot and replacing a few assignments with values.

If you get errors you can still come back and ask why something is not working.
If we just post the code you won’t learn from it, you will only be able to exactly rewrite the code. I mean, how do you want to adjust the code? Every change may cause errors, and if you don’t want to face and solve errors, you won’t be getting anywhere.

So one key thing in programming is to encounter errors, read the error message, try to solve it and if you cannot do it on your own, ask for specific help and understand the ideas and solutions.

That is important. If you don’t wanna face these problems or don’t want to learn how to solve errors, programming might not be what you’re looking for. And really, we’re talking about syntax erros in this case, not about semantical and logical errors.

I am not qualified to instruct individuals on C# let alone C# scripting, but here you go

 //C# constants?
 //example
 public const float PowerLevel = 10000; //all const must be declared with a value.. i think
 
 

	//C# variables 
	private bool flashlightOn = false; // <access level> <var type> <name> with option to set it at declaration = <value>;
 
 
 //C# functions
 void Update()
 {
	 
	 //Stuff
	 
	 //local vars
	 int LocalIntVal, val2, val3;
	 val2 = 4;
	 val3 = -4;
	 LocalIntVal = PowerLevel * val2 + val3; 
	
	 
 }

Created this script for you, I have tested it, it has no errors for me. Hope you like my code…

using UnityEngine;
using System.Collections;

public class MYCLASSNAME : MonoBehaviour {
float PowerLevel = 10000;
float LightPowerUse = 1;
float ChargeSpeed = 0.5f;
int InstantCharge = 3;
//Private variables:
    private bool  flashlightOn = false;
    private float SpotAngleMax = 80;
    private float LightLevel2 = 0f;
    private int LightLevel3 = 0;
    private float MaximumPowerLevel = PowerLevel;
    private float AngleMultiplier = 1;
    private float NormalAngle = SpotAngleMax;
    private int ShowAngle = 0;
    private float PowerUsage = 0;
    private float PowerCharge = 0;
    private bool  PowerUse = false;
void  Update (){
    PowerUsage = 0;
    PowerCharge = 0;
    PowerUse = false;
    //Light max spot angle
    if(SpotAngleMax > 179)
    {
        SpotAngleMax = 179;
        print("Spot and show angle can max be 179 degrees");
    }
    //Enable the flashlight
    if(Input.GetAxis("Fire1"))
    {
        LightLevel2 = PowerLevel / 10000;
        GetComponent.<Light>().intensity = LightLevel2;
        //GetComponent.<Light>().spotAngle = LightLevel2 * SpotAngleMax;
        GetComponent.<Light>().spotAngle = SpotAngleMax * AngleMultiplier;
        PowerLevel -= LightPowerUse * AngleMultiplier;
        PowerUsage += LightPowerUse * AngleMultiplier;
        PowerUse = true;
        flashlightOn = true;
        if(flashlightOn == true && Input.GetAxis("Fire2"))
            AngleMultiplier = 0.1300007f;
    } else {
        GetComponent.<Light>().intensity = 0;
        flashlightOn = false;
        AngleMultiplier = 1;
    }
    //print("Light percent: " + intens /100 + "%");
    if(PowerLevel < 0)
        PowerLevel = 0;
    if(PowerLevel > MaximumPowerLevel)
        PowerLevel = MaximumPowerLevel;
    //Charging the battery
    if(PowerUse == false && PowerLevel < MaximumPowerLevel)
    {
        PowerLevel += ChargeSpeed + PowerUsage;
        PowerCharge += ChargeSpeed;
        PowerUsage -= ChargeSpeed;
    }
    LightLevel3 = PowerLevel / 100;
    //Instant charge the battery
    if(Input.GetKeyDown(KeyCode.X))
    {
        if(InstantCharge > 0)
        {
            InstantCharge -= 1;
            PowerLevel = MaximumPowerLevel;
            print("Instant charge used");
        } else {
            print("No more charges");
        }
    }
    //Scroll wheel
    if(Input.GetAxis("Mouse ScrollWheel") > 0)
    {
        if(AngleMultiplier < 2.229999f && flashlightOn == true)
        {
            AngleMultiplier += 0.01f;
        }
        //print("Scroll up");
    } else if(Input.GetAxis("Mouse ScrollWheel") < 0 && flashlightOn == true)
    {
        if(AngleMultiplier > 0.1300007f)
        {
            AngleMultiplier -= 0.01f;
        }
        //print("Scroll down");
    }
    //Resetting the angle multiplier when pressing down scroll wheel
    if(Input.GetAxis("Fire3"))
    {
        if(flashlightOn == true)
            AngleMultiplier = 1;
    }
    ShowAngle = SpotAngleMax * AngleMultiplier;
}
void  Reset (){
    PowerLevel = 10000;
    InstantCharge = 3;
    flashlightOn = false;
}
void  OnGUI (){
    //Show the power level
     GUI.Box( new Rect(100, 0, 100, 20),"Power: " + LightLevel3 + "%");
    //Show instant charges
     if (InstantCharge > 0)
     {
            GUI.Box( new Rect(100, 20, 100, 20),"Instant: " + InstantCharge);
     } else {
         GUI.Box( new Rect(100, 20, 100, 20),"no charges");
     }
    //Show flashlight angle
     if(Input.GetAxis("Fire1"))
     {
         if(ShowAngle == 80)
         {
             GUI.Box( new Rect(0, 40, 100, 20),"Angle: def");
         } else if(ShowAngle == 179)
         {
             GUI.Box( new Rect(0, 40, 100, 20),"Angle: max");
         } else if(ShowAngle == 10)
         {
             GUI.Box( new Rect(0, 40, 100, 20),"Angle: min");
         } else {
             GUI.Box( new Rect(0, 40, 100, 20),"Angle: " + ShowAngle);
         }
     } else {
         GUI.Box( new Rect(0, 40, 100, 20),"Flash off");
     }
    //Show power usage
     if(PowerUsage > 0)
     {
         GUI.Box( new Rect(100, 40, 100, 20),"Usage: " + PowerUsage + "%/s");
     } else if(PowerLevel == MaximumPowerLevel)
     {
         GUI.Box( new Rect(100, 40, 100, 20),"Max Charge");
     } else {
         GUI.Box( new Rect(100, 40, 100, 20),"Charge: " + PowerCharge + "%/s");
     }
}