- Home /
Cannot find the length of an array
Hello! I am getting an error when I try to find the length of an array! All I am trying to accomplish with this script is spawn a random gameobject from an array. To do that I need to find the length of an array, yet for some reason Unity won't let me do that. The error I am getting is: CS0117 UnityEngine.GameObject[] does not contain a definition for 'Length'
Here is my code, also note I have tried to just print the length of the array to the console out of the instantiate and just in the start function, it resulted in the same error.
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] objects;
// runs spawn func
void Start () {
Spawn();
}
//spawns in a random item from an array and then does it every .5 seconds.
void Spawn()
{
Instantiate(objects[Random.Range (0, objects.Length)], transform.position, Quaternion.identity);
Invoke ("Spawn", .5f);
}
}
Thanks for viewing (and hopefully helping)!
This is... odd. I'm not seeing anything wrong with your code... this answer shows pretty much the same thing...
The only thing I can think of is that perhaps there's some weird internal $$anonymous$$onoBehaviour property called objects
that is interfering? That shouldn't be the case because yours should be the one in scope. But try rena$$anonymous$$g objects
to something else? Complete stab in the dark I know.
if you are still having issues you could always use a list ins$$anonymous$$d.
Add using System.Collections.Generic;
to your namespaces and just use List<GameObject>
ins$$anonymous$$d.
So, although I could not fix this error still. I am too lazy to do the research on lists, and as I am the only one working on the game I just declared a public value that I could change in the editor depending on the size of the array, obviously not what you want to have to do, but it being so $$anonymous$$or I don't really $$anonymous$$d. If anyone else has any ideas I would be open to them.
Also Dave, thanks for the response and indeed I have tried different names and that doesn't seem to fix the problem. I will have to contact Unity's support $$anonymous$$m and see if perhaps I am just missing a package or something.
Thanks all!
In this kind of issue, you should provide Unity version, hardware, OS, etc...
I recreated the essence of this in a new C# script, ran it, worked fine.
using UnityEngine;
using System.Collections;
public class ArrayTest : $$anonymous$$onoBehaviour {
public GameObject[] objects;
void Start () {
Debug.Log(objects.Length);
}
}
No errors, console/debug.log displays 0
Answer by benbendixon · Sep 06, 2015 at 03:29 PM
@bunny83 @wibble82 Yes I am sure I haven't changed anything. I even created a new project and tried running a debug line that would just print the length of a simple array and that didn't work. I have not "given up" on this question, I just simply don't have anything to add. I have been trying to work with a support member. Feel free to mark the question as unreproduceable.
@benbendixon: Well, you haven't addressed any of the questions we asked back.
Like:
What exact Unity version do you use?
Where exactly do you get that error? In Unity, in $$anonymous$$onoDevelop, in another IDE you use like VisualStudio?
I asked you to copy and paste the exact and complete error message from the console that includes the file name and line / column numbers.
Apart from that there are at least 2 different "XXX doesn't contain a definition for YYY" errors. There is CS0117 and CS1061. CS1061 would usually apply to your case but you say you get CS0117 which applies to cases where you try to access a member in a "base class" which is completely impossible for array types since you can't derive classes from an array type.
If you ignore all the other counter questions, at least tell use which exact Unity version you're using. For example i still use "5.1.1f1 Personal". You can see that version in the "About" screen.
Nobody is able to reproduce your problem and i don't even know any way to purposely generate that exact error.
@Bunny83 I haven't answered any of the questions because I am already in contact with unity support and this appears to be something that only they would be able to figure out since it is their software and there is an issue with my install or something like that. If you would like me to answer them I will.
I am running Unity 4.6.3f Personal
It happens in line 16 and I already gave the error message, I would copy and paste but I am much farther along in my game and am no longer using this script because of the error.
I get the error in unity and monodevelop.
Answer by Cryptomane · Sep 06, 2015 at 04:07 PM
Works fine for me, even if the array was empty.
The only thing that comes to my mind might be some corrupted library in your project, OR ...
Do you have a "GameObject" class in your project?
No I do not, as I said it is something wrong with my install of unity obviously. I am working with support.
Answer by OceKat3 · Oct 27, 2019 at 10:32 PM
@benbendixon This is how I fixed the problem for myself: Use an uppercase L in "Length" instead of a lowercase one. The unity documentation makes it seem like you should use a lowercase L, which I find weird.
Probably this one. I agree that the documentation is a little bit misleading as they describe both UnityScript's Array class (which does not exist anymore) and native C# built-in arrays. The code example is about the built-in arrays while the actual page is about the Array class. The "length" property of that class actually was lower case.
If you want to look up information on standard / built-in C# / .NET framework classes, head for the $$anonymous$$SDN documentation
Your answer
Follow this Question
Related Questions
Instantiate from array into array? 2 Answers
How Do I Add An Instantiated Object To An Array? 3 Answers
Spawning according to screen width 3 Answers
2D Array of GameObjects... 1 Answer