Pragma strict errors

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).

Thanks for helping out :slight_smile:

-Rob

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’.

//again, simplified, but...

var messages : Array = Array();
message.Push("Message1");
[COLOR="red"]var msg : String = messages[0];[/COLOR]

Edit:
So I guess my question is, how do I convert an Object to a String?

As for the new error: “as string” or “.ToString()” at the end as Array holds references of Object, not String (as the error tells you as well)

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.

It would be generally better if you used List<> instead of Array, then you wouldn’t have to worry about casting, plus it would be quite a bit faster.

–Eric

So, will I have to write my own Vector3 class simply so I can extend a ToString() method, and a Custom Parse method?

Something like:

class RobsVector3 extends Vector3
{

  function ToString() : String
  {
    return x + " " + y + " " + z;
  }

  static Function Parse (object : Object) : Vector3
  {
    var s : String [] = Regex.Split(" ",object.ToString())
    var newVec : Vector3 = Vector3(float.Parse(s[0]),float.Parse(s[1]),float.Parse(s[2]));
    return newVec;
  }
}

Noted.

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)

Hey all-- Thanks.

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 myRects = [Rect(0,0,10,10), Rect(0,0,25,25)];

You can just write

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.)

–Eric

1st example: Does the first example work with #pragma strict on? To me, it doesn’t look like it would.

2nd example: Thats good to know-- I thought that List was something Unity built in, but I guess not.

Also, I have confirmed that the following does build and run on the iPhone.

var myRects : Rect [] = Array(Rect(0,0,10,10),Rect(0,0,25,25)).ToBuiltin(Rect) as Rect[];

Yes, it does…there’s no dynamic typing there at all.

That’s pretty roundabout though…better to just declare the Rect array directly without involving Array.

–Eric

To me that seems strange-- I would have thought that

var myRects = [Rect(0,0,10,10), Rect(0,0,25,25)];

would have been implicit because there is no type defined with myRects-- I actually would have through that would have been synonymous with

var myRects : Object [Rect(0,0,10,10), Rect(0,0,25,25)];

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.

–Eric