Got a few bugs that I’m not entirely sure how to fix:
BallController.js(50,43): BCE0022: Cannot convert ‘UnityEngine.Component’ to ‘DontGoThroughThings’.
//in a class
private var dontGoThroughThings : DontGoThroughThings;
function Awake () : void
{
[COLOR="red"] dontGoThroughThings = GetComponent(DontGoThroughThings); //line 50[/COLOR]
}
and then there is this one too:
Assets/Scripts/BaseGame/DataController.js(563,32): BCE0022: Cannot convert ‘System.Array’ to ‘String[ ]’.
//function is more complicated than this, but this is for example purposes
function GetNames () : String []
{
var strings : String [] = ["b","a"];
var temp : Array = Array();
for (var x : int = 0 ; x < strings.length-1; x++)
{
temp.push(strings[x]);
}
temp.Sort();
[COLOR="red"] names2 = temp.ToBuiltin(String); //line 563[/COLOR]
return names2;
}
}
The above error messages are holding me back from leaving #pragma strict in my scripts (I otherwise use it to fix everything except these).
First error: You are lacking the typecast ie GetComponent(DontGoThroughThings) as DontGoThroughThings;
second one the same but not fixable potentially with Unityscript as that requires the prefix cast, you can’t do array casts with the postfix “as” typecasting, and that prefix cast is only available in C#
First Error: Awesome, thanks, fixing it everywhere now.
Second Error: Is there any way to make some sort of a dynamically typed class to fix this? I’ve only ever really done this once, and that was in C++ three years ago. I forget what they are called, but maybe something like…
class MyConverter extends MyExtension implements MyImplementation <type>
{
static function Convert (array : Array) <type>
{
var myNewArray : <type> [array.length];
for (var x : int = 0; x < array.length; x++)
{
myNewArray[x] = array[x];
}
return myNewArray;
}
}
P.S. Any idea how I might fix this one:
Assets/Scripts/Menus/Menu.js(97,40): BCE0022: Cannot convert ‘Object’ to ‘String’.
I would have tried that, but I mimstakenly assumed “String” was a value, not a reference (if I had realized that I wouldn’t have asked the question)-- so my next question is, how do I do that for values?
I saw your post about checking the manual for what things return, and I most certainly will (my real problem was just that I didn’t know about the ‘as’ operator-- I’ve already corrected a good 50 errors, extrapolating what I’ve learned and applying it to Hashtables, to System.Collections.DictionaryEntry, etc).
So, given a value inside an object, how would I cast that?
Through the corresponding parsing like int.Parse( messages[0].ToString() )
if it is an own struct of you, you will have to create own parsers, likely To and From string.
as you don’t have the prefix cast you can’t cast around structs otherwise.
Also, if you need regular removes for some reason, use LinkedList<> instead, that will give you a listnode object back upon adding which you can use for direct removal (list.remove otherwise has to search the whole list for a remove)
After some 200-250 errors of putting " as ", deleting unused variables (and in some cases, fixing spelling mistakes), and converting a lot of Arrays to System.Collections.Generic.Lists, everything checks out O.K.
A few things I did want to follow up on:
This actually /does/ work in the unity editor (maybe not actually on the iPhone? I’m about to try it out):
var myRects : Rect [] = Array(Rect(0,0,10,10),Rect(0,0,25,25)).ToBuiltin(Rect) as Rect[];
The above is actually how I have been doing it in my code, since I have not for the life of my been able to figure out the syntax for this:
var myRects : Rect [] = {Rect(0,0,10,10),Rect(0,0,25,25)};
Also, for anyone stumbling on this, I found the syntax (somewhere) for generic classes:
//instantiate
var myList : System.Collections.Generic.List.<Vector3>;
//initialize
myList = System.Collections.Generic.List.<Vector3>();
var myList : List.<Vector3>;
myList = List.<Vector3>();
You don’t even need to add “import System.Collections.Generic” to do that. (However, you do need to include that import line if you want to write “var myDictionary = new Dictionary.<String, int>”, which is kind of inconsistent.)
It’s type inference at work. It’s the same thing that makes
var foo = 5;
be defined as an int, and
var foo = "blah";
be defined as a string. The compiler sees an array of Rects, so it makes myRects be defined as Rect[ ]. You can do
var myRects : Rect[] = [Rect(0,0,10,10), Rect(0,0,25,25)];
if you like…there’s no difference. Personally it’s enough for me to see an array of Rects to make it quite clear what the type is; it’s not like I’m going to mistake it for anything else when looking at that code, so adding Rect[ ] is kind of redundant here. Up to you though.