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 /
  • Help Room /
avatar image
0
Question by Theacesofspades · Sep 05, 2015 at 05:21 PM · gameobjectarraysetactiveenable

How to enabled a specific gameObject in array?

I have an array of gameobjects called chunks[ , , ]. All the gameobjects in the array are inactive. I need to set a specific gameobject enabled. How can I do this? Im using:

 world.chunks[chunkX, chunkY, chunkZ].SetActive(true);

This will not work. I just get a null reference exception. Can anyone help me? Thank you

Comment
Add comment · Show 5
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 Adamcbrz · Sep 05, 2015 at 05:38 PM 0
Share

How are you adding them to the array because it looks like the index is not set to a gameobject. So I think your logic is sound with calling SetActive on an item in the array but that value is null so that assumes it has not been set or it has been destroyed.

avatar image Theacesofspades · Sep 05, 2015 at 06:06 PM 0
Share

I go through the array to fill it.

 void CreateChunks ()
      {
          chunks=new GameObject[$$anonymous$$athf.FloorToInt(worldX/chunkSize),
                                $$anonymous$$athf.FloorToInt(worldY/chunkSize),
                                $$anonymous$$athf.FloorToInt(worldZ/chunkSize)];
          
          for (int x=0; x<chunks.GetLength(0); x++)
          {
              for (int y=0; y<chunks.GetLength(1); y++)
              {
                  for (int z=0; z<chunks.GetLength(2); z++)
                  {
                      
                      chunks[x,y,z] = Instantiate(chunk,new Vector3(x*chunkSize,y*chunkSize,z*chunkSize),new Quaternion(0,0,0,0)) as GameObject;
                      chunks[x,y,z].transform.parent = transform;
                      chunks[x, y, z].SetActive(false);
  
                      ChunkScript newChunkScript= chunks[x,y,z].GetComponent<ChunkScript>();
                      
                      newChunkScript.worldGO=gameObject;
                      newChunkScript.chunkSize=chunkSize;
                      newChunkScript.chunkX=x*chunkSize;
                      newChunkScript.chunkY=y*chunkSize;
                      newChunkScript.chunkZ=z*chunkSize;
                      
                  }
              }
          }
      }

avatar image Adamcbrz · Sep 05, 2015 at 07:42 PM 0
Share

That looks ok...is this a race condition? Are you sure that CreateChunks is called before you try to access it? Are these objects destroyed in anyway?

avatar image Theacesofspades · Sep 06, 2015 at 04:58 PM 0
Share

In not destroying them im just setting them to not active

avatar image hexagonius · Sep 06, 2015 at 08:16 PM 0
Share

just a lucky guess, but maybe world is null, the rest looks fine.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Set Active Random GameObject from array (JavaScript) 1 Answer

Getting collision for individual gameobjects in an array and changing properties of other gameobjects from that array 0 Answers

Access GameObject Array and Turn Render on via the key pressed - Keys are already named based on game object. 0 Answers

How to make a gameObject appear when I press spacebar down? 1 Answer

[HELP C#] Making Gameobject 1 equal Prefab 2 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