user1741587 user1741587 - 3 years ago 133
C# Question

How can i add items to array to the start and to the end?

In the top of a script:

private Array sections;


Then:

var bm3 = new ExtrudedTrailSectionBM3();
var section = bm3;
section.point = position;
section.matrix = transform.localToWorldMatrix;
section.time = now;
// using 2 sections
sections.Unshift(section); // back
sections.Unshift(section); // front


But Unshift is for java script and i'm using csharp.

Answer Source

Firstly, I would only ever suggest using arrays for public vars to be edited in the inspector, for anything other than that they are too primitive.

If you are specifically needing to or wanting to use an array you will need to recreate it every time to add data or otherwise resize it.

Use lists though :D

public class temp : MonoBehaviour {
public List<string> myList;
// Use this for initialization
void Start () {

    //initialize your list
    myList = new List<string> ();
    //add to your list
    myList.Add("some junk");
    string temp = "some more junk";
    myList.Add (temp);
    //add a new item at a specific index
    myList.Insert(0,"The new first item");
    //remove the first item: "The new first item"
    myList.RemoveAt(0);
    //remove everything
    myList.RemoveAll();


}

Note: Please consider correctly formatting your questions in the future as it becomes difficult for others to find information in the future resulting in duplicate questions :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download