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 Pyrox · Feb 12, 2014 at 06:57 PM · c#gameobjectarraystatic

GameObjects static array NullReferenceException

Hello, everyone, I am quite new to Unity and I am trying to port a game I made some time ago in it. I am facing a problem with a static array of GameObjects. I get no errors in the editor, but when I try to access it, I get a NullReferenceException. Here is the code I used to create the array:

 static GameObject[] blockArray = new GameObject[10];
 
     void start() {
         blockArray[0] = GameObject.Find ("Block1");
         blockArray[1] = GameObject.Find ("Block2");
         blockArray[2] = GameObject.Find ("Block3");
         blockArray[3] = GameObject.Find ("Block4");
         blockArray[4] = GameObject.Find ("Block5");
         blockArray[5] = GameObject.Find ("Block6");
         blockArray[6] = GameObject.Find ("Block7");
         blockArray[7] = GameObject.Find ("Block8");
         blockArray[8] = GameObject.Find ("Block9");
         blockArray[9] = GameObject.Find ("Block10");
     }

What am I doing wrong?

Comment
Add comment · Show 1
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 DajBuzi · Feb 12, 2014 at 07:06 PM 0
Share

$$anonymous$$aybe try to change array into a list?

1 Reply

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

Answer by Jessy · Feb 12, 2014 at 07:07 PM

start doesn't get called automatically. Start does.

I recommend using PascalCase for everything except backing fields (which are a stupid concept and shouldn't exist, but that's what we've got), and parameters. If you use camelCase, then you have to change case when converting from a field to a property. Just treat every variable like a property with get and set sharing the access of the property itself, until you want to change one of them.

Also, you can't count on a field initializer on a MonoBehaviour. Move that into Awake.

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 Pyrox · Feb 12, 2014 at 07:25 PM 1
Share

Thank you, I can't believe I have spent so much time on this and it was because of a lower case letter.

avatar image Jessy · Feb 12, 2014 at 07:54 PM 0
Share

It's not totally your fault; Unity should do a better job of telling you about this without having to resort to debugging.

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to modify specific GameObjects in an array of GameObjects? 1 Answer

C# SetActive GameObject Array 2 Answers

Alter a GameObjects(Clone) array from another script C# 0 Answers

C# Adding Components From Other Gameobjects 3 Answers

Attaching GameObject and ParticleSystem to C# script 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