Converting Array Object to int (or finding an alternative)

Using Javascript and developing for Android.

I’m using arrays to create a resizable list of coordinates that will describe a path that the player will draw on a 6x6 grid. While I could technically rewrite the code to use static arrays, I would like to see if I can get resizable arrays working.

Here’s the code:

``````    var pathX = new Array();
var pathY = new Array();

function Start () {

//I cut a bunch of stuff out here that's irrelevant
}

function InPath (xx, yy) {
for (var i = 0; i < pathX.length; i++) {
if (pathX _== xx && pathY *== yy) {*_
``````

* return true;*
* }*
* }*
}

function NearPath (xx : int, yy : int) {
* if (pathX[pathX.length-1] == xx) {*
* if (Mathf.Abs(pathY[pathY.length-1] - yy) <= 1) {*
* return true;*
* }*
* } else if (pathY[pathX.length-1] == yy) {*
* if (Mathf.Abs(pathX[pathX.length-1] - xx) <= 1) {*
* return true;*
* }*
* } else {return false;}*
}
It returns this error:
Assets/LevelControl.js(68,53): BCE0051: Operator ‘-’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘int’.
Because the array class stores everything as object types, it can’t do the subtraction. Is there a way to make this work with arrays, or should I just convert over to a static array?

There’s two ways this can be handled.

1. use `System.Convert.ToInt32(System.Object obj)` before you use the subtraction operator:

var x : System.Object = 100;
var y = System.Convert.ToInt32(x) ;

Debug.Log("Type: " + x.GetType()); // should now be an System.Int32 aka int
Debug.Log("Value: " + x);

var z = y - 50;

2. Use `List.< int >` instead of Array, which will avoid conversion altogether.

// goes up at the top of the script file
import System.Collections.Generic;

//…

var list : List. = new List.();