Checking empty array (Transform[])?

Hey all-

I looked over the forums with no luck. I must be overlooking an obvious problem here. I am wanting to check if an array, which is defined as a member variable (var thisArray : Transform[ ]) has anything in it, but I keep getting this error:

NullReferenceException: Object reference not set to an instance of an object
UnityScript.Lang.Extensions.get_length (System.Array a)

Essentially, I have one script which fills the array and another that checks to see if there is anything in that array. It is a simple goal manager (waypoint).

Thanks for any help.

you need to define the array after declaring it like so

var thisArray : Transform[];
thisArray = new Transform[10] // to create an array of size 10

As for whether it is empty you would have to loop through all elements of the array and see if they are null

function isEmpty(arr : Transform[]) : boolean {
   for(i = 0; i < arr.length; arr++) {
      if(arr[i] != null)
          return false;
   }
   return true;
}

you may want to look into lists as well if you don’t know the size of the array to begin with or you need to resize it later. For lists you can just check the size to know if it is empty.

Thanks ivonki-

But I need the array to change size? Essentially, I am having the character look for goals, decide on one of some possible goals, and if there are no goals then wander and continue looking for goals until it finds another set of possible goals.

Thanks

-s

EDIT -

I will look into lists. Thanks

-s

EDIT -

Are lists only available in C#?

-s

Ok … So I tried a generic array. (var possibleGoals = new Array():wink: but attempting to access this when it has a no members still throws an error… What am I doing wrong? It would seem that the array exists, but I would think it would be a fairly common function to look to see if the list is null.

thanks again

-s

Growing and shrinking arrays is generally a bad idea. It’s slow and can create memory fragmentation. You’re best off just creating the array at the maximum size it’s ever going to reach and then only use what you need when you need it.

Array can be used though. Before accessing it’s elements be sure to check its length.

if(myArray.length > index)
{
    myArray[index].DoStuff();
}

Don’t use Array, use List.

var transformArray = new List.<Transform>();

–Eric

OK … so I suppose for me that lists are out of the question. I am stuck to using javascript for now… just don’t have time to rewrite my code in C#. So I am attempting to use javascript arrays via this article…

It says that the “Array()” should be dynamic. And since I will not have that many objects in the array it should be fine. However, I am still throwing an exception when the array contains nothing. ie

if (thisArray.length > 0)
{
doSomething();
}

any ideas? It seems sort of silly that you can’t check to see if an array has no size, but is still declared? NO?

-Thanks

-s

You can use List in UnityScript.

Array syntax for length is Array.Count… also, search the Unity docs for Array info, not Javascript Array info on the internet as its different in some subtle ways.

BUT: Most importantly, Lists work fine in UnityScript… Eric posted initialisation code above. Did you not see it?

Lists are definitely the best option for you. Fast, Lean, Dynamic and can hold Unity objects and be referenced directly without dynamic casting.