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 Storm1906 · Oct 30, 2016 at 10:29 AM · c#instantiatedestroyarray of gameobjects

Gameobject array

Hello, I recently hit a wall, and even if I found an alternative, I would like to know how to do it this way.

Imagine we have an empty GameObject called "floor". This one instantiates children GameObjects as cubes, which are stored in an array : While...{ floorCell[I] = Instantiate(prefab,...)}

These cells are moving (downward referring to the player), and when they arrive behind the player I would like them to disappear, and the array to adapt itself : While...{ floorCell[I] = floorCell [I+1]} floorCell[endOfArray] = Instantiate...

If I do it this way, it works, but each time the first GameObject stays, and accumulate each time. If in the while loop I add : Destroy(floorCell[I].gameObject) I end up with an error saying I can't manipulate GameObjects not existing.

Mainly practising C, I'm used to create arrays and manipulate pointers, but in C# and unity I don't really know how to adapt myself :P

Thanks in advance for your help :)

Comment
Add comment · Show 3
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 Ambrose998800 · Oct 30, 2016 at 10:51 AM 0
Share

To access the first gameobject of the array, you have to use floorCell[0] (cause the first object in a array/list/transform hierarchy is always 0 and not 1). So, if your first element stays, it seems you start destroying the gameobjects by floorCell[1] ins$$anonymous$$d floorCell[0].

Check that...

avatar image Storm1906 · Oct 30, 2016 at 11:36 AM 0
Share

@Ambrose998800

I didn't wan't to be annoying with long code samples (by the way my array is actually 2 dimensional, but it was simpler to explain this way), but it looks like that :

if(the first block [0] gets behind){ for(int i=0; i

avatar image Storm1906 · Oct 30, 2016 at 11:39 AM 0
Share

@Ambrose998800

I don't know if you see the entire message, it looks like it was cut off, I'm using the mobile site version and it's not always working well...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to replace 2 objects. Destroying assets is not permitted to avoid data loss. 0 Answers

Instantiate Previous Prefab in Array 0 Answers

Instantiate an object and destroy it after given time 2 Answers

How do I destroy a GameObject/Script that instantiates other objects, without destroying the objects it has previously instantiated? 0 Answers

Destroy prefabs after they have been instantiated (C#) 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