trying to get a handle on arrays...

I’m pretty good with array logic in other software I’ve worked with, but I’m having some trouble here.

Here’s the premise of what I want to happen. As you run into objects, there should be a list of collected objects on the screen.

Here’s the basics of what I tried…

var numberOfObjects = 1;
var objectNamesArray : String[];
objectNamesArray[1] = "First Object";

And then in a GUI function -

var n = 1;
while (n <= 1)
     {
     GUI.Label(Rect(10, 10 + (16*n),100,100),objectNamesArray[n]);
     n += 1;
     }

So far, this should draw at LEAST that first object name, however I’m told that the array index is out of range. I’ve tried a lot of little tweaks and fixes to no avail.

Any suggestions? Thanks!

Keep in mind that arrays use a zero based index. That is, the first element is retrieved with someArray[0], not someArray[1]. In your example, your array’s last (and only) element is at index zero, but you’re trying to access it with n which has a value of one, hence the index out of range exception.

Wow…it was really THAT easy. I think I have been staring at code too long!

For anyone else with the problem or who wants to do something similar - that was the fix. Thanks!

Alright, next phase to the same problem…trying to make it add to the array. Just in order to make it happen…how could I make it so if I press the enter key, there is a new dimension added to the array?

I would think it would be:

if (Input.GetButton("EnterKey"))
     {
     numberOfObjects += 1;
     objectNamesArray[numberOfObjects]="New Object";
     }

and then the second part of my code should take care of itself, seeing as now it will do that while loop and n will be be able to increase by another number, creating that new object name 16 below the first…

but it isn’t working…any thoughts?

if (Input.GetButton("EnterKey")) 
     { 
     objectNamesArray.Push("New Object"); 
     }

Easier to code and you dont need to worry about that extra variable, in your loop substitute that var for (objectsNamesArray.length).

I was creating this array as a built in array - just looked a little more familiar. Tried it as a JS array instead, and I get an error saying that the object reference is not set to an instance of an object…

this happens any time i call the array as a javascript array. If I call it as a built in array, it works fine…but I can’t seem to add to the array.

Thoughts?

Most likly that means you have a Null object reference. Check the inspector panal and make sure all your variables are assigned.

Yes, I am for some reason getting a Null object reference…but I can’t figure out why. There is nothing in the inspector thats unassigned. In fact, I have taken EVERYTHING out of the script except this:

var objectArray = new Array("Why isn't it");
objectArray.Add(" working?");

function Update()
     {
     print(objectArray);
     }

And what is returned is printed is “Null”. Shouldn’t this print “Why isn’t it working?”???

That is ALL that is in the script, and it was referencing that print command in the update function when it returned the error.

I thought simplifying would make it work and then I could rebuild with a better understanding…but to no avail. Help! Thanks!

try objectArray.Push(" working?“); or objectArray.Unshift(” working?");

Thanks…as you advised, I tried Add, Unshift, and Push. They all return “Null”.

any other thoughts?

Actually it prints “Why isn’t it, working?” But it does work…

–Eric