Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Matrix problem

Discussion in 'Scripting' started by Afisicos, Jul 6, 2012.

  1. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
    I have an "out of index error" in this script I don't know why :(

    Code (csharp):
    1. var dimensiones : Vector2;
    2. var Matriz = new GameObject[dimensiones.x, dimensiones.y];
    3. var mesh : Mesh;
    4. var j : int;
    5. var i : int;
    6.  
    7. function Start()
    8. {
    9.    for (i = 0; i < dimensiones.x; ++i)
    10.    {
    11.         for (j = 0; j < dimensiones.y; ++j)
    12.         {  
    13.             Debug.Log("Plano X="+i+"Y="+j);
    14.             Matriz[i,j].name = i.ToString()+j.ToString();     //ERROR HERE IN EXECUTION
    15.             Matriz[i,j].AddComponent(MeshFilter);
    16.             Matriz[i,j].AddComponent(MeshRenderer);
    17.             Matriz[i,j].AddComponent(MeshCollider);
    18.             Matriz[i,j].transform.position = Vector3(i,0,j);
    19.             Matriz[i,j].GetComponent(MeshFilter).mesh = mesh;
    20.         }
    21.     }    
    22. }
    Someone knows why?
     
  2. Boss

    Boss

    Joined:
    Jun 27, 2012
    Posts:
    133
    var dimensiones : Vector2;

    var Matriz = new GameObject[dimensiones.x, dimensiones.y];

    var mesh : Mesh;

    var j : int;

    var i : int;

    String : j;

    String : i;



    function Start()

    {

    for (i = 0; i < dimensiones.x; ++i)

    {

    for (j = 0; j < dimensiones.y; ++j)

    {

    Debug.Log("Plano X="+i+"Y="+j);

    Matriz[i,j].name = i.ToString();
    Matriz[i,j].name = j.ToString();

    Matriz[i,j].AddComponent(MeshFilter);

    Matriz[i,j].AddComponent(MeshRenderer);

    Matriz[i,j].AddComponent(MeshCollider);

    Matriz[i,j].transform.position = Vector3(i,0,j);

    Matriz[i,j].GetComponent(MeshFilter).mesh = mesh;

    }

    }

    }

    //---------------------------------------------------

    Try this. All i did was seperate that line for better String transform. Hope this helps!

    UPDATED! Forgot a ;
     
    Last edited: Jul 6, 2012
  3. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
    m,mmm, Thanks but is isn't the problem. The error is alive.
     
  4. Boss

    Boss

    Joined:
    Jun 27, 2012
    Posts:
    133
    What does the error say?

    I forgot ; on that script try now I updated it.
     
  5. Moraleidahgo

    Moraleidahgo

    Joined:
    Mar 3, 2012
    Posts:
    107
    Not sure if it has anything to do with it, but your for looks weird, have you tried i++ and j++ instead of ++i and ++j?
     
  6. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Your problem is likely here...
    Code (csharp):
    1.  
    2. var Matriz = new GameObject[dimensiones.x, dimensiones.y];
    3.  
    Vector2 x and y are float. The compiler is going to round to the nearest integer. For example:

    If dimensiones.x=3.000000001, that value will be round to 3 for the array dimension, which means valid indexes for that dimension of the array are i=0, i=1, i=2. The loop the counter will reach i=3, and 3 < 3.000000001 is obviously true, so it will go one more time through the loop, but index i=3 is out of range.

    You can't use floats to initialize the size of the array. You should either define integer dimensions, or round the numbers yourself.

    I'm actually a bit surprised that this even compiles. It should, at the very least, throw a warning, telling you that x and y are floats being reduced in precision to integers. But, perhaps not with this compiler? I haven't actually checked to see if it will or not.
     
    Last edited: Jul 6, 2012
unityunity