Also, like an array, we must specify which type of object we want a particular List to store. The first thing to understand is that a List has the ability to store any type of object, just like an array. To mimic adding a new element to an array, we would need to create a whole new array with the desired number of elements and then copy the old elements.You can easily add and remove elements from a List. An array is of fixed size and unchangeable.Here are the basics of why a List is better and easier to use than an array: It is up to the developer’s preference and task. Look at some forum sites related to C# and Unity, and you’ll discover that plenty of programmers simply don’t use an array unless they have to they prefer to use a List. Using a List instead of an array can be so easier to work with in a script. Jump into the Unity scripting documentation and search for GameObject.FindGameObjectsWithTag:Īs you can see, GameObject.FindGameObjectsWithTag is a special built-in Unity function that takes a string parameter ( tag) and returns an array of GameObjects using this tag. In fact, we are just declaring the new array variable to store a collection of GameObject in the scene using the "car" tag. You are probably wondering why the last example, shown here, looks different: GameObject carsInTheScene = GameObject.FindGameObjectsWithTag("car") GameObject carsInTheScene = GameObject.FindGameObjectsWithTag("car") Īs you can see, we can store different types of data as long as the elements in the array are of the same type. Let’s store the same names using a C# array variable type: public string familyMembers = new string There is a much cleaner way of storing lists of data. The name variable suggests that it’s a changing element. The problem starts when you don’t know how many student names you will be storing. We can print and assign new values to them. Simple! Just create a few variables and name them student1, student2, and so on: public string student1 = "Greg" Imagine we want to store a number of student names. We can use arrays to store lists of values in a single variable. An array stores a sequential collection of values of the same type, in the simplest terms.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |