Search Unity

Array GetIndex Function

Discussion in 'Scripting' started by Delconis, Sep 1, 2011.

  1. Delconis

    Delconis

    Joined:
    Nov 27, 2010
    Posts:
    13
    I've made a function to get the index by compareing a value with all the values in the array it returns the index of the item found in the array. But for some reason it's giveing me the following error.
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. scriptServerGUI.GetNameIndex (System.String item, System.String array) (at Assets/Server/scriptServerGUI.js:204)
    3. scriptServerGUI.Update () (at Assets/Server/scriptServerGUI.js:131)
    I don't know exactly what the error in tales. I'm guessing my input values are not passable. or null for that matter. But I don't understand why. I am using the JS array because it's dynamic and I can Push and Remove unlike Builtin. But regardless i'm having issues with this line and i don't know why. Any help would be appreciated.

    Public Variable
    Code (csharp):
    1. var selectedItems   = new Array (Transform);
    Debug and Using Function
    Code (csharp):
    1.                         Debug.Log("Remove from Selection");
    2.                         Debug.Log("Current Select Object Transform: " + selected);
    3.                         Debug.Log("Selected Items: " + selectedItems);
    4.                         var indexNumber = GetIndexArray(selected.transform,selectedItems);
    5.                         selectedItems.RemoveAt(indexNumber);
    GetIndexArray Function
    Code (csharp):
    1. function GetIndexArray(item, array)
    2. {
    3.     //check array type
    4.     //if builtin convert js
    5.  
    6.     //Debug.Log( item + " - " + array);
    7.     for (var i; i <= array.length; i++)
    8.     {
    9.         if(array(i) == item)
    10.         {
    11.             return i;
    12.            
    13.         }
    14.     }  
    15. }
    My Debug Outputs
    Code (csharp):
    1. Current Select Object Transform: Grass Tile (UnityEngine.Transform)
    2. UnityEngine.Debug:Log(Object)
    3. scriptServerGUI:Update() (at Assets/Server/scriptServerGUI.js:130)
    Code (csharp):
    1. Selected Items: UnityEngine.Transform,Dirt Tile (UnityEngine.Transform),Grass Tile (UnityEngine.Transform),Grass Tile (UnityEngine.Transform)
    2. UnityEngine.Debug:Log(Object)
    3. scriptServerGUI:Update() (at Assets/Server/scriptServerGUI.js:131)
     
  2. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    at the top:
    if( array == null ) return -1;

    at the bottom:
    return -1;

    you could get passed a null array, and if the item isnt in the array the function returns nothing.

    its pretty standard to return -1 when something doesnt exist.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    For dynamic arrays, use List instead of Array. Use IndexOf to get the index of an item in the array.

    Code (csharp):
    1. import System.Collections.Generic;
    2.  
    3. function Start () {
    4.     var transformList = new List.<Transform>();
    5.     transformList.Add(transform);
    6.     Debug.Log (transformList.IndexOf(transform));
    7. }
    Always, always declare the types of variables in function declarations:

    Code (csharp):
    1. function GetIndexArray (item : Transform, array : Array)
    And always, always declare the type of variables elsewhere, either explicitly or implicitly with a value:

    Code (csharp):
    1. for (var i = 0; i <= array.length; i++) // fine
    2. for (var i : int; i <= array.length; i++) // technically OK, although I think making it clear that you're starting with 0 is good practice
    3. for (var i : int = 0; i <= array.length; i++) // fine
    Code is much faster and less likely to have bugs when you use the type, and in some cases you don't have a choice (such as when doing iOS/Android publishing).

    --Eric