Hi, i try to integrate some javascript code into a unity-script and have a scoping problem: Code (csharp): function Update () { } function month(numdays, abbr) { this.numdays = numdays; this.abbr = abbr; } var monthList = new Array(); var i = 0; monthList[i++] = new month(31, "Jan"); The problem is that "this" refers to the script, iso the funtion object. I get the error "numdays is not a member of "TestScript". How should this be coded correctly?
1. You must declare variables that are meant to be used all over the scripts, outside the functions. You can not just use them and then they are "magically there" 2. Are you sure you are using valid .NET code on the month part? UnityScript (unity's JS) is not website JS and does not offer any of its functionality and libraries
Ok, so i cannot use "ordinary" JavaScript. Does Unity JavaSript offer a way to define structures or objects? The part of the listed code tries to instantiate initialise a custom defined structure named "month", containing a string and an integer.
Use classes instead. Code (csharp): class Month { var numdays : int; var abbr : String; function Month (n : int, a : String) { numdays = n; abbr = a; } } --Eric
Thanks! with the Class definition, and changing the way the Array is used it now builds. Code (csharp): var monthList = new Array(); monthList.Add(new Month(31, "Jan"));