Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by ErickP · Nov 10, 2012 at 04:02 PM · gameobjectarrayinspectornull

How to access gameobject array set in inspector?

I´m having trouble with a somewhat simple task but could not find exactly what is happening in the forum...

The idea is to create an array of gameObjects which will be populated via Inspector tab.

The code below is returning null for the civilianPrefabs[0] Debug call, even though I have set the GameObject array with size 1 and with a prefab as the first element.

 var civilianPrefabs : GameObject[];
 
 function Start () {
   civilianPrefabs = new GameObject[2];
   Debug.Log(civilianPrefabs[0]);
 }

Another question: is it really necessary to instantiate the gameObject array with its size? As it has already been populated by the inspector I don´t see the need for that..

Comment
Add comment · Show 2
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 Nercoe · Nov 10, 2012 at 04:11 PM 0
Share

Have you got the array to work? Can you access and populate it via the inspector panel?

avatar image ErickP · Nov 11, 2012 at 02:48 PM 0
Share

still nothing... I have already populated it with 2 different prefabs in the inspector panel

2 Replies

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

Answer by ErickP · Nov 11, 2012 at 08:58 PM

The issue at hand was a Unity bug: the initial script had a GameObject[] array populated with the inspector tab. When trying to access any index number Unity would return an "index out of range" error.

The issue was solved by creating a new script with exactly the same code and replacing it in the gameobject.

Thanks everyone for the answers!

Comment
Add comment · 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
3

Answer by Kmulla · Nov 10, 2012 at 04:36 PM

In the inspector tab you can change the size of the array by changing the number 1 to the number of GameObjects you want in the array.

alt text

As for your code. In the first line of the function Start() you overwrite the Gameobject array with a new one, which contains two empty GameObjects, which is why you get a null reference.

You can se it yourself by moving the Debug log to the first line of Start(), where it'll return your GameObejct.


skærmbillede 2012-11-10 kl. 17.29.07.png (9.5 kB)
Comment
Add comment · Show 6 · 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 ErickP · Nov 11, 2012 at 02:40 PM 0
Share

I have taken the initialization line out, but now I am getting an "array out of range" error.. Same thing happens when I increase the size of the array in the inspector

avatar image ErickP · Nov 11, 2012 at 02:48 PM 0
Share

If I instantiate civilianPrefabs[0] it strangely creates the object in the Scene but generates the "array index is out of range" error, stopping the execution afterwards

avatar image Kmulla · Nov 11, 2012 at 06:27 PM 0
Share

If I recall correctly, the "array out of range" error occurs when you try to acces an element in the array with a higher index than the length of the array. If it's the Debug.Log(civilianPrefabs[0]); that gives the error, you might have forgotten to populate the first object in the array. If that's not the case, will you please post your code then? :)

avatar image ErickP · Nov 11, 2012 at 07:20 PM 0
Share

In this case I am not populating the array with the script... It should already have 2 elements (like you have shown in the picture above). for some reason the script is not recognizing civilianPrefabs[0] even when I should have 2 elements in this array . the current code is: var civilianPrefabs : GameObject[]; function Start () { Debug.Log(civilianPrefabs[0]); }

avatar image Kmulla · Nov 11, 2012 at 07:34 PM 0
Share

I've tested the code in my own project and it works just fine. Is your gameObjects, in the array, destroyed? Otherwise I'm not sure I'm able to find the error.

I bet your project contains other scripts. Are you accessing the array through any of them?

Show more comments

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

12 People are following this question.

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

Related Questions

how to check for GameObject is null in array with random 1 Answer

Simple Looping drag and drop game 1 Answer

Prefab not loading in data from Inspector 1 Answer

Can a GameObject inside of an array be made "accessible" from the editor? 2 Answers

Array element coming as not null 2 Answers


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