Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by benbendixon · Sep 01, 2015 at 06:49 PM · gameobjectinstantiatearrayarraysspawning

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)!

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Sep 01, 2015 at 06:55 PM 0
Share

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.

avatar image kjakubison · Sep 01, 2015 at 07:12 PM 0
Share

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.

avatar image benbendixon · Sep 02, 2015 at 02:42 AM 0
Share

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!

avatar image Mikilo · Sep 02, 2015 at 03:21 AM 0
Share

In this kind of issue, you should provide Unity version, hardware, OS, etc...

avatar image getyour411 · Sep 02, 2015 at 03:33 AM 0
Share

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

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Sep 06, 2015 at 05:40 PM 0
Share

Did you try to look through a step-by-step debugger?

avatar image Bunny83 · Sep 07, 2015 at 01:56 AM 0
Share

@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.

avatar image benbendixon · Sep 07, 2015 at 04:00 AM 0
Share

@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.

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image benbendixon · Sep 06, 2015 at 05:45 PM 0
Share

No I do not, as I said it is something wrong with my install of unity obviously. I am working with support.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Oct 27, 2019 at 10:34 PM 0
Share

The documentation? Can you provide the link?

avatar image Bunny83 Mikilo · Oct 29, 2019 at 11:15 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Checking if a position is occupied in 2D? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges