- Home /
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?
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;
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.
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();
Answer by Hamilcar-games · Aug 12, 2018 at 11:51 AM
using system;
Array.clear(arrayname,0,arrayname.length);
Good. This works fine. But "Clear" and "Length" with capital letter.
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;
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.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613182038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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