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 Al3XXX · Jan 31, 2015 at 11:41 AM · c#array

How to Clear Array or Remove it's items

Hi I have script, which moves parent independently of children. It works for static scene, but i have children which Instantiates permanently. because of that I need to clear childs array before new iteration.

 using UnityEngine;
 using System.Collections;
 
 public class MoveParentToChilds : MonoBehaviour {
     public Transform[] childs;
     public float ControlDistance = 10f; //Расстояние между потомком и родителем на котором сработает скрипт
     private int j;
     private float MaxValue = 0f;
     // Use this for initialization
     public void ResetDistance () {
         childs = gameObject.GetComponentsInChildren<Transform>();
         foreach (Transform child in childs) {
             if (Mathf.Abs (child.localPosition.x) > ControlDistance) {
                 float Value = Mathf.Abs (child.localPosition.x) - ControlDistance;
                 if (Value > MaxValue) {
                     MaxValue = Value;
                 }
                 child.parent = null;
             }
         }
         j = 1;
         if (transform.position.x < 0) {
             j = 1;
         }
         float transX = transform.position.x - MaxValue * j;
         transform.position = new Vector3 (transX, transform.position.y, transform.position.z);
         foreach (Transform child in childs) {
             child.parent = transform;
         }
     }
 }

So the question is why I can't remove element of Array childs (is it array or not?) childs.Clear() not working. And how to remove 1st item of childs, because 1st item is Parent?

Comment
Add comment · Show 13
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 nu-assets · Jan 31, 2015 at 12:38 PM 1
Share

An array is basically a representation of sequential data in the memory. The array has a fixed size by definition, so you can not clear it nor change its size. What you can do is reinitialize it or set some items to null. If you want to benefit from a dynamic data structure I would recommend that you use a generic list:

 List<Transform> childs;
avatar image RudyTheDev · Jan 31, 2015 at 01:27 PM 1
Share

What @wittwism said: use List < Transform >. You can manually rebuild the array or use Array class, but it is almost always easier and more efficient to use a List.

avatar image DESIMA · Jan 31, 2015 at 01:29 PM 0
Share

its more expensive to use a List rather than a array

avatar image nu-assets · Jan 31, 2015 at 01:35 PM 0
Share

The array has a smaller memory footprint and accessing indexed data has a complexity of O(1). So overall: When you have a fixed amount of Items use an array. But when you need a dynamically growing/shrinking structure use a List.

avatar image Derek-Wong · Jan 31, 2015 at 01:37 PM 0
Share

in your case, you just need to reset the array by assign a new value to it, you don't really need to clear it. simply

childs = gameObject.GetComponentsInChildren();

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Hamilcar-games · Aug 12, 2018 at 11:51 AM

using system;

Array.clear(arrayname,0,arrayname.length);

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 WILEz1975 · Jun 30, 2019 at 02:23 PM 0
Share

Good. This works fine. But "Clear" and "Length" with capital letter.

avatar image
2

Answer by DESIMA · Jan 31, 2015 at 07:09 PM

If I understand you question correctly you want to clear element of the childs array?

so then:

 public Transform[] childs;
 
 for ( int i = 0; i < childs.Length; i++)
 {
    childs[i] = 0;
 }
 
 //or 
 
 for ( int i = 0; i < childs.Length; i++)
 {
    childs[i] = null;
 }
 
 

Using the childs.Clear() method does not word because its a method that can clear certain elements of an array.

see here https://msdn.microsoft.com/en-us/library/system.array.clear(v=vs.110).aspx

so you can use the system Array class if you had certain elements you wanted to remove. if you wanted to remove all elements then the code I listed.

if you want to remove first item of childs it is

 childs[0] = 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 Al3XXX · Feb 02, 2015 at 07:04 PM 0
Share

I think this will not remove whole element itself with index. I mean that:

 string[] myObjArray = new string[5] { "A", "B", "C", "D", "E" };
         Debug.Log(myObjArray.Length);// 5
         myObjArray[0] = null;
         Debug.Log(myObjArray.Length);// 5 ins$$anonymous$$d of 4



It's not exactly what I need. Anyway thanks. List type of Array was very helpfull. I will use it.

avatar image
0

Answer by DESIMA · Jan 31, 2015 at 05:15 PM

There is a difference between Array.Clear() and then the array you are using.

https://msdn.microsoft.com/en-us/library/system.array.clear(v=vs.110).aspx

thus the Array.Clear(); works by removing a certain element og elements from you childs array.

if you wanted to clear all elements or had an index in the array you wanted to remove then you do

 for (int i = 0; childs.Length; i++)
 {
    childs[i] = null // or
    childs[i] = 0; 
 
 }

both should work

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Set Current Array GameObject to Active 1 Answer

How to check for an empty array? 1 Answer

Deleting an Element from an Array. 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