Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[CLOSED] Converting c# to javascript

Discussion in 'Scripting' started by Yuzi1221, Jul 24, 2020.

  1. Yuzi1221

    Yuzi1221

    Joined:
    Jul 20, 2020
    Posts:
    9
    how can I convert this to javascript Im getting errors like insert ";" and expecting ")" and unexpected token ")"


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import System.IO;
    4.  
    5. var express = require("express");
    6. var events = require("events");
    7. var app = express();
    8. var eventer = new events.EventEmmiter();
    9. var comments = [];
    10.  
    11. function Start () {
    12.    
    13. }
    14.  
    15. function Update () {
    16.  
    17.         PlayerData playerdata = new PlayerData();
    18.         playerdata.position =  transform.position;
    19.  
    20.         var json = JsonUtility.ToJson (playerdata);
    21.  
    22.         Debug.Log (json);
    23.  
    24.         var path = "Assets/SaveFile.json";
    25.         FileStream filestream = new FileStream (path, FileMode.Create);
    26.         using (StreamWriter writer = new StreamWriter (filestream)) {
    27.             writer.Write (json);
    28.         }
    29. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  3. Yuzi1221

    Yuzi1221

    Joined:
    Jul 20, 2020
    Posts:
    9
    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
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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:
    Code (CSharp):
    1. PlayerData playerData =
    To this:
    Code (JavaScript):
    1. var playerData: PlayerData =
    And do the same for the other declarations.
     
    Last edited: Jul 24, 2020
    Joe-Censored likes this.
  5. Yuzi1221

    Yuzi1221

    Joined:
    Jul 20, 2020
    Posts:
    9
    I fixed everything but this part of the code. It says its missing a semicolon on first line.

    Code (JavaScript):
    1.         var StreamWriter: writer = new StreamWriter (filestream) {
    2.         writer.Write (json);
    3.         }
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You can get rid of those curly braces, since the
    using
    statement is gone:
    Code (JavaScript):
    1. var StreamWriter: writer = new StreamWriter(filestream);
    2. 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.