[CLOSED] Converting c# to javascript

how can I convert this to javascript Im getting errors like insert “;” and expecting “)” and unexpected token “)”

#pragma strict

import System.IO;

var express = require("express");
var events = require("events");
var app = express();
var eventer = new events.EventEmmiter();
var comments = [];

function Start () {
   
}

function Update () {

        PlayerData playerdata = new PlayerData();
        playerdata.position =  transform.position;

        var json = JsonUtility.ToJson (playerdata);

        Debug.Log (json);

        var path = "Assets/SaveFile.json";
        FileStream filestream = new FileStream (path, FileMode.Create);
        using (StreamWriter writer = new StreamWriter (filestream)) {
            writer.Write (json);
        }
}

That isn’t C# though. Looks like JS already, which Unity doesn’t actually support anymore unless you’re still using 2017.x or one of the early 2018.x versions.

Yes Im using Unity 2017 and I already tried to convert it into javascript that is the code I came up with but it doesn’t work

Again, the code you posted already is JavaScript. There’s no conversion needed, unless you mean you want to convert it to C#.

Are you maybe mixing up these two languages?

Edit:
Nevermind, I just noticed the C# code syntax inside the Update function.

In JavaScript (more specifically, UnityScript), the types of variables/references are declared after the names. So you’d change this:

PlayerData playerData =

To this:

var playerData: PlayerData =

And do the same for the other declarations.

1 Like

I fixed everything but this part of the code. It says its missing a semicolon on first line.

        var StreamWriter: writer = new StreamWriter (filestream) {
        writer.Write (json);
        }

You can get rid of those curly braces, since the using statement is gone:

var StreamWriter: writer = new StreamWriter(filestream);
writer.Write(json);

It’s worth mentioning though that the purpose of the C# using keyword in this context is to free up system resources after closing the file-stream.

I’ve never used UnityScript before, so I don’t know if it has an equivalent to this, or if it just isn’t needed here.