Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 invadrzim · Mar 08, 2014 at 05:53 AM · gameobjectinstantiatearraydestroy

Destroying 2D Array of Instantated Objects

I'm having trouble with destroying a 2D array of instantiated Prefabs. I searched around here and there were many questions that were very similar but their answers simply do not work for me.

I'm making a grid of plane objects at run time. What i want to be able to do is generate a grid of a certain size, then be able to regenerate the grid at another size. the problem is, despite looping through the grid and calling Destroy on each one, it just keeps making a new grid on top of the grid already there.

Generation Code: private void GenerateGrid() {

         for (int zz = 0; zz < gridWidthandHeight; zz++)
         {
             for (int xx = 0; xx < gridWidthandHeight; xx++)
             {
                 grid[xx, zz] = (GameObject)Instantiate(_squarePrefab);
 
                 grid[xx, zz].transform.position = topLeftPosition +
                     new Vector3(squareDimensions.x * xx, 0, squareDimensions.z * zz);
             }
         }
     }

Destruction Code:

 private void DestroyGrid()
     {
         for (int zz = 0; zz < gridWidthandHeight; zz++)
         {
             for (int xx = 0; xx < gridWidthandHeight; xx++)
             {
                 if (grid[xx, zz] != null)
                 {
                     Destroy(grid[xx, zz]);
                 }
             }
         }
     }

I call the Destroy function before the generate function every time i call to make the grid:

 public void BuildGridObject(int widthAndHeight)
     {
         if (widthAndHeight % 2 != 1)
         {
             widthAndHeight += 1;
         }
 
         gridWidthandHeight = widthAndHeight;
 
         grid = new GameObject[gridWidthandHeight, gridWidthandHeight];
 
         position = this.gameObject.transform.position;
         squareDimensions = _squarePrefab.renderer.bounds.size;
 
         topLeftPosition = position -
             new Vector3(
             (gridWidthandHeight * squareDimensions.x) / 2,
             0,
             (gridWidthandHeight * squareDimensions.z) / 2) +
             new Vector3(
                 squareDimensions.x / 2,
                 0,
                 squareDimensions.z / 2);
 
         DestroyGrid();
         GenerateGrid();
 
         bounds = new Bounds(position, new Vector3(squareDimensions.x * gridWidthandHeight, 1, squareDimensions.z * gridWidthandHeight));
     }

alt text

I have A set to make a 21x21 grid, S to make a 31x31 grid, D to make a 41x41 grid and F to make a 51x51 grid.

Every time I press one of those keys it should get rid of the grid that's there and make the one, but its obviously not doing that.

I'm building this game as a way to get myself familiarized with Unity, so there must be something i'm missing, but i can't figure out what. The other answers to similar questions were things like destroying the rigid body and not the game object, but my grid is a grid of game objects, by everything I've found this should work.

grid.jpg (91.4 kB)
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
1
Best Answer

Answer by robertbu · Mar 08, 2014 at 07:54 AM

I see two problems here. On line 10, you initialize 'grid'. If grid contained a reference to an existing grid, you just blew that reference away. You would want to do a destroy before you created the new grid. The second issue is a bit more subtle. Since you are passing in 'widthAndHeight', and setting that for the class, if your width and height changes, then you would either not destroy some game objects, or you could get an out of bounds error. You can fix this by moving DestoryGrid() call to line 7 (before you set 'gridWidthAndHeight') so that you are using the values before the array is resized. In fact, it would be better if the DestoryGrid() took its sized directly from the array rather than depend on this variable.

And if you move DestoryGrid() back to line 7, you will need to make it contingent on 'grid' not being null.

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 invadrzim · Mar 08, 2014 at 04:03 PM 0
Share

that was exactly what the problem was, i can't believe i missed that. It works beautifully now thank you so much!

avatar image
1

Answer by Benproductions1 · Mar 08, 2014 at 07:51 AM

On line 10 you're creating a new array of GameObjects as your new grid. When you create a new array for reference types, each reference is defaulted to null. So when you create the new grid, you're also forgetting the old grid.

Then later on line 25 you try to destroy the old grid, which you have forgotten by creating a new grid.

The solution is simply: Destroy the grid before you create a new one.

Note the difference between "create" and "generate"

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

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

21 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

Related Questions

using destroy with an array of GameObjects (C#) 1 Answer

How can I destroy objects and instantiates new in Array? 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Referencing an instantiated object with a global variable 0 Answers

Trash Collection For Instantiated Game 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