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 chrisjw42 · Jan 23, 2014 at 12:00 PM · c#gameobjectarraydynamic

Emptying and filling Arrays on command? C#

Hi All,

I'm looking to solve a problem, and my understanding of arrays is not good enough to do so, here's what I'm after:

An array which fills up with X number of GameObjects, any of which could be destroyed at any time. Under certain circumstances (let's say, a button is pressed), I'd like to be able to fill up the empty spaces of the array with more GameObjects.

I don't think it's important but the objects will be randomly placed and will all be identical.

Is there some kind of array or combo of array & loop that can achieve this? I would imagine there is some way of filling all the empty positions in an array.

Thanks!

P.S I'm working in C#

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by joelfivat · Jan 23, 2014 at 12:15 PM

Is that what you are looking for ?

 GameObject[] array = new GameObject[x];
 
 for(int i=0; i<array.Length; i++){
 
     if(array[i]==null){
         array[i] = your game object
     }
 
 }

You can find a great documentation about arrary in C# here : http://www.dotnetperls.com/array

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 chrisjw42 · Jan 24, 2014 at 02:26 AM 0
Share

Yes!! That looks like the basis that I need to solve it. Just to clarify, will an array position be automatically converted to 'null' if the object is destroyed? Or will I need to include code to do that.

Thanks!

avatar image joelfivat · Jan 24, 2014 at 08:13 AM 0
Share

Destroying the object won't set the array position to null.

Actually, since there is a reference in the array pointing to the gameObject, this will prevent the object from being destroyed. You won't see it in the scene anymore, but it will still be in memory, so yes, you need to set it to null manually :)

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

19 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

Related Questions

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

Make an array of all visible objects 1 Answer

2D array of GameObjects C# 2 Answers

How to put gameObjects to the list? 4 Answers

How do i change a sprite when another gameobject with the same prefab is colliding / is near 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