Need Help on this Array error!

Please help! How come when I compile the below code under window platform, it will run. When i change the platform to mobile, it gives me error “Type ‘Object’ does not support slicing”. I believe the error is regarding this array:

craterData[x*cratertex.width + y].a

What is a better way to write that code?

var saved : float[,,];
private var tData : TerrainData;
var cratertex : Texture2D;
var craterData;
var xRes: int;
var yRes: int;
var layers: int;
 
function Start () 
{
    tData = Terrain.activeTerrain.terrainData;
    yRes = tData.alphamapHeight;
    xRes = tData.alphamapWidth;
    layers = tData.alphamapLayers;
    craterData = cratertex.GetPixels();
    saved = tData.GetAlphamaps (0, 0, xRes, yRes);
}
 
function OnApplicationQuit () 
{
    tData.SetAlphamaps (0,0,saved);
}
 
function Update () 
{
    if (Input.GetMouseButtonDown(0))
    {
       var g : int = Mathf.Lerp(0, xRes, Mathf.InverseLerp(0, tData.size.x, mouse.mousepos.x));
       var b : int = Mathf.Lerp(0, yRes, Mathf.InverseLerp(0, tData.size.z, mouse.mousepos.z));
       g = Mathf.Clamp(g, cratertex.width/2, xRes-cratertex.width/2);
       b = Mathf.Clamp(b, cratertex.height/2, yRes-cratertex.height/2);
       var area = tData.GetAlphamaps(g-cratertex.width/2, b-cratertex.height/2, cratertex.width, cratertex.height);
       for (x = 0; x < cratertex.height; x++) 
       {
         for (y = 0; y < cratertex.width; y++) 
         {
          for (z = 0; z < layers; z++)
          {   
              if (z == 1)
              {
                 area [x,y,z] += craterData[x*cratertex.width + y].a;
              }
              else
              {  
                 area [x,y,z] -= craterData[x*cratertex.width + y].a;  
              }  
          }
         }
       }
       tData.SetAlphamaps (g-cratertex.width/2,b-cratertex.height/2,area);
    }
}

In your game, does cratertex.GetPixels() consistently return the same sized array? That’s where the issue might be.

If cratertex.GetPixels() returns, say, an array of size 1,000 each and every time then it shouldn’t be the bug. However, if on every frame it might return a different size, then craterData should be initialize as a dynamic array, so it’s size can change dynamically. You can do that by doing this:

var craterData : Color[];  // Declare the array

Give that a try and let me know if it helps the issue.