I have been changing my code from C# into JS (job mentorship in web development). At the moment I am off on my playerscript.js. Code (JavaScript): var float: speed = 0.1F; var myInt : Score = 0; var myInt : HighScore = 0; var myint : Lives = 5; var ExplosionPrefab : Transform; // Use this for initialization function Start () { HighScore = PlayerPrefs.GetInt("HighScore"); } // Update is called once per frame function Update () { //movement speed of ship if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition; transform.Translate(-touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0); transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z); //screen boundary var distance = (transform.position - Camera.main.transform.position).z; var leftBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).x + (playerSize.x/2); var rightBorder = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance)).x - (playerSize.x/2); var bottomBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).y + (playerSize.y/2); var topBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, distance)).y - (playerSize.y/2); transform.position = (new Vector3 ( Mathf.Clamp (transform.position.x, leftBorder, rightBorder), Mathf.Clamp (transform.position.y, bottomBorder, topBorder), transform.position.z) ); } if (Score > HighScore) // set HighScore from current is higher HighScore = Score; // To save it PlayerPrefs.SetInt("HighScore", HighScore); void OnGUI() //error { GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(3, 3, 3)); GUI.Label (new Rect(10, 10, 120, 20), "Score: " + playerScript.Score.ToString ()); GUI.Label (new Rect(10, 30, 120, 20), "HighScore: " + playerScript.HighScore.ToString ()); GUI.Label (new Rect(10, 50, 120, 20), "Lives: " + playerScript.Lives.ToString ()); } void OnTriggerEnter2D(Collider2D) //error { playerScript.Lives--; var other : ShipScript = gameObject.GetComponent("player"); StartCoroutine(DestroyShip()); } IEnumerator DestroyShip() //error { Instantiate(ExplosionPrefab, transform.position, Quaternion.identity); gameObject.renderer.enabled = false; transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z); yield WaitForSeconds(0.1f); if (playerScript.Lives > -1) gameObject.renderer.enabled = true; else Application.LoadLevel("gameover"); } }
no offense... but... why? I mean, I can get writing unityscript. But why take already existing C# code and convert it to unityscrypt? As for your errors, they're happening where you're declaring functions. In C# your declare them as: accessModifier returnType name(paramType paramName) Code (csharp): public int SomeFunc(bool inputValue) in UnityScript/Javascript it is: accessModifier function name(paramName:paramType) : returnType Code (csharp): public function SomeFunc(inputValue:bool) : int AccessModifier isn't required in either, if you don't include it, private is implied for C#, and I think public is implied for unityscript. Not sure for sure on the US/JS side of that. ReturnType isn't required for US/JS. You don't include it at all if there is NO return type (void in C#), and otherwise, the compiler will figure it out on its own. Although, that does make it less readable, so I always have put return types in no matter what. so Code (csharp): void OnGUI() becomes Code (csharp): function OnGUI() Also, you have your class fields all messed up with: Code (csharp): var float : speed = 0.1f; var myInt : Score = 0; var myInt : HighScore = 0; var myInt : Lives = 5; var ExplosionPrefab : Transform; variable declaration in US/JS is: var variableName:variableType = value; 'speed' is not a type, float is. So you should probably reverse those. you declare multiple variables 'myInt' and typed what appear to be names. I would say reverse those, but 'myInt' isn't a type either, but I assume that was supposed to be 'int'. ExplosionPrefab looks right. I have no idea how you got 3 completely different syntactical organization all in 5 lines of code. Programming languages are designed to be consistent, not arbitrary.
You should realize that "JS" in Unity is not Javascript, and is typically referred to as Unityscript to reflect that. Yes, the default in Unityscript is public. You can specify void if you like; it's just another type. In most cases it's not necessary to declare the return type, void or otherwise, if the compiler can figure it out, but in certain cases it can't (e.g. recursion), so in that case you'll get an error message, and you'll have to supply the type in order for the code to compile. --Eric
Thanks a lot. Yes, I know it is UnityScript and I Told my mentor why I stopped since it's not web development javascript. I just want to get it done with and work on what I have now.
it's a little concerning that the so called mentor doesn't understand the difference between programming and web development. I believe the closest unity gets to be "web development" is a web player build target and pinching variables from the web page it's embedded into.
Depends on your definition of Web development. Especially back-end web development is as pure programming as Unity is. And even some Javascript frameworks are more full blown programming frameworks then scripting frameworks (AngularJS, Knouckout etc) And of course you have typescript nowadays which allows normal Javascript to be developed close to how C# works.
he just said it so I could learn more than one language- but I know html and css. Meh....anyways- thank you, guys.