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 oneWaveStudios · Apr 24, 2012 at 05:23 PM · gameobjectnullreferenceexceptionarrays

Select every GameObject in GameObject Array

Dear UnityPro's/more intelligent people than me,

I am experiencing some problems with my arrays, and I think it is my mistake of wrong defining the array/wrong handling of the array:

 static var RallyPoint : GameObject[];
 RallyPoint = new GameObject[50];

This is the array, now I am putting gameobjects into the array with another script:

 GameSettings.RallyPoint[GameSettings.BuildingID] = Instantiate(RallyPointObject,GameSettings.BuildingIDs[GameSettings.BuildingID].transform.position,Quaternion.Euler(-90,0,0));



Works very well so far, though I don't know if I did it the correct way(?) Now in another script I want to let all the Gameobjects disappear from the scene:

 for(var i : GameObject in GameSettings.RallyPoint)
     {
         i.renderer.enabled = false;
     }

And this is where I get an error/where it is not working.

NullReferenceException: Object reference not set to an instance of an object

As far as I get it the function can't find a gameobject at the position of the array. But I clearly defined one above? So where is my mistake? Or did I define wrong?

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

2 Replies

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

Answer by save · Dec 14, 2012 at 04:52 PM

There are three optional solutions to the null reference that comes to mind,

1.

f BuildingID never increments when you assign GameObjects to your array.

 for (GameSettings.BuildingID = 0; i<GameSettings.RallyPoint.Length; GameSettings.BuildingID++) {
     GameSettings.RallyPoint[GameSettings.BuildingID] = Instantiate(RallyPointObject,GameSettings.BuildingIDs[GameSettings.BuildingID].transform.position,Quaternion.Euler(-90,0,0));
 }

However I suspect that BuildingID is a number you work with in another way later in your scripts, as you would equally be able to just call GameSettings.RallyPoint.Length in this case. Use a temporary incrementing int in that case when assigning to the array.

2.

f RallyPoint doesn't assign all GameObjects at once, you then call for all positions in the built-in array. Either you can create an ArrayList which is resizable or rebuild your built-in array when you add or remove rally points. Choose arrays carefully.

3.

he more obvious regarding the error message but not as probable, some or all of the objects does not have a renderer component.

 if (i.GetComponent(Renderer)) i.renderer.enabled = false;


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
0

Answer by masood-t · Dec 14, 2012 at 03:09 PM

I have the same problem

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 LyokoJames · Dec 14, 2012 at 04:09 PM 0
Share

Use the add comment button to comment rather than using an answer

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

6 People are following this question.

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

Related Questions

Null reference when accessing GameObject in the Array(C#) 1 Answer

NullReference when accessing GameObject in array (C#) 1 Answer

OnCollisionEnter GameObject throws NullReferenceException 0 Answers

Problem with array on gameobjects 2 Answers

Rigidbody: NullReferenceException: Object reference not set to an instance of an object 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