- Home /
Getting the size of an array of GameObjects
I hope this isn't a dumb question - but I am having trouble getting the length of an array of gameobjects using c#. The array is populated in the inspector. But for some reason objects.Length, objects.Size, and objects.Count do not work.
Thank you for any help!
public GameObject[] objects;
can you give us the complet code (all text in the script)?
Answer by Loius · Jul 07, 2013 at 10:32 PM
Do not work in what way? Because objects.Length is the way to do it.
If there are any errors they are likely interfering with expected results.
The size \ length of an array can't be changed. Once created the size of an array will be constant.
If you need to change it you have to create a new array and copy the old elements over. You can use System.Array.Resize which does that for you.
However when you frequently need to add \ remove elements you might want to use a generic List ins$$anonymous$$d.
Next time when you have a question you should ask a question and not necropost such an old question with a comment
Answer by aldonaletto · Jul 07, 2013 at 11:23 PM
Length is a property, not a method, thus there are no parenthesis. In C# it's capitalized:
int howMany = objects.Length;
In JS it's low case:
var howMany = objects.length;
EDITED: JS actually accepts both, lowercase and uppercase (see the comments in this answer and in the others).
It's uppercase in JS. .NET is .NET regardless of language. You may be able to use lowercase as an alternative (I haven't tested it), but if so that's an aberration and should be avoided for the sake of consistency, since properties in .NET are always uppercase. (Sadly properties in the Unity API are not.)
Yes, I've just tested this: JS accepts both, uppercase and lowercase.
Your answer
Follow this Question
Related Questions
transform array in javascript 2 Answers
problem accesing arrays 2 Answers
Multidimensional GameObject Arrays 1 Answer
Create a Word Game like Scrabble in Unity? 0 Answers
Creating an inventory/weapon array and ability to scroll through it 1 Answer