Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

What am I doing wrong here?

Discussion in 'Scripting' started by renman3000, Apr 16, 2012.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Hi,
    Why would the first for set up record the positions I want, however the second for enemy, does not record any positions?


    Code (csharp):
    1.  
    2. #pragma strict
    3. var heroObjectArray : Array;
    4. var heroObject : GameObject[];
    5. var enemyObjectArray : Array;
    6. var enemyObject : GameObject[];
    7. var heroObjectPos : Vector3[];
    8. var enemyObjectPos : Vector3[];
    9.  
    10. function Start () {
    11.    
    12.     heroObjectArray  = new Array(heroObject);  
    13.     enemyObjectArray  = new Array(enemyObject);    
    14.    
    15.     for(var b : int = 0; b <= heroObjectArray.length; b++)
    16.     {
    17.         ///record all positions of b objects.
    18.         heroObjectPos[b] = heroObject[b].transform.position;
    19.     }
    20.     for(var d : int = 0; d <= enemyObjectArray.length; d++)
    21.     {
    22.         ///record all positions of d objects.
    23.         enemyObjectPos[d] = enemyObject[d].transform.position;
    24.     }  
    25. }
    26.  
     
  2. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Are you sure that enemyObject[] has any entries at all? It wouldn't make sense for the code not to work, since it is identical to the one that works. Something must be wrong with the second array.
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Yes. It has 8 members all assigned.

    When i check the inspector, after start, the heroObjectPos array is filled. The enemyObjectPos array has all vector0 values.

    Not sure.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Ok, I am finding this very frustrating.

    I have a question tho which hopefully can provide some insight.


    Code (csharp):
    1.  
    2. var heroObjectArray : Array;
    3. var heroObject : GameObject[];
    4. var enemyObjectArray : Array;
    5. var enemyObject : GameObject[];
    6. var heroObjectPos : Vector3[];
    7. var enemyObjectPos : Vector3[];
    8. var heroObjectManager : GameObject;
    9. var heroObjectManagerData : heroManagerScript;
    10.  
    11. function Start () {
    12.    
    13.     heroObjectManagerData = heroObjectManager.GetComponent(heroManagerScript);
    14.     while (heroObjectManagerData.settingScene == true)
    15.     yield;
    16.    
    17.     heroObjectArray  = new Array(heroObject);  
    18.     enemyObjectArray  = new Array(enemyObject);    
    19.    
    20.     for(var b : int = 0; b <= heroObjectArray.length; b++)
    21.     {
    22.         print("b " + b);
    23.         ///record all positions of b objects.
    24.         heroObjectPos[b] = heroObject[b].transform.position;
    25.     }
    26.     for(var d : int = 0; d <= enemyObjectArray.length; d++)
    27.     {
    28.         print("d " + d);
    29.         ///record all positions of d objects.
    30.         enemyObjectPos[d] = enemyObject[d].transform.position;
    31.     }  
    32. }
    33.  

    my print outs for "b" always go one value over the size.

    I get the error that the array for b is out of range.



    Why would that be?
     
  5. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Never use <= in conjunction with lenght. The lenght of an array is always higher -- in an n element array, the elements are indexed from 0 to n-1
     
  6. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    You need to learn basic scripting. Once you learn this, then you will know about zero-based arrays and what will happen when accessing the [length] element.
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Thanks.
     
    Last edited: Apr 17, 2012