I wanted to learn more about arrays and summarize what I learned here. Arrays are extremely important and useful in almost any game in Unity, and I know I will need them for my independent project. I currently have very little understanding of them because most of the tutorials I've done lately have had that section automatically completed.
What I Already Know Arrays are ways to create a set of objects, names, numbers, or whatever else you want to put in a group. This is useful for spawning enemies randomly from different spawn points, or random encounters with different enemies that are all in an array together. You have to set up an array and define what is in it, but then from there you can use many different functions to choose random items from within it, or to act upon each of the objects in the group. However, I don't quite remember how all of this happens or the extent to which this can be used. What The Unity Scripting API Says It describes arrays as allowing you "to store multiple objects in a single variable". Arrays are indicated by the type of objects in the array followed by two square brackets - for instance GameObject[] enemies. Then we can use a foreach loop to do something to every game object in that group, such as instantiate all of them. This could be helpful for loading in levels. The only property an array has is length, or how many objects can fit in that array. You can also choose whether the array is serializable or nonserializable. This just means you can choose whether or not you'll see all of the items in the array in the inspector. There are also other methods to join arrays, remove items, shift the order of items, and add items. What I Learned From A Four Minute Video This video does a great job of explaining how arrays work. It shows two methods of putting items in your arrays. You can either, go one by one (ie array[0] = 1, array[1] = 5, array[2] = 17) or just list the items in order (ie {1, 5, 17}). It also explains the "i" or "iterator" variable, that I never quite understood. Basically this variable just acts as a stand in for the array's length so that then we can use foreach loops to do an action to each object in the array. Was This Worth Learning? Short answer: yes. I have a way better understanding of arrays now. However, I think I still have a lot to learn, but I also think that learning is going to have to come from trial and error as I code. However, this is going to make creating games with many enemies much easier. Summary
0 Comments
Leave a Reply. |
AuthorHi, I'm Abi, a DSA student who likes games, drawing, writing, and acting. Archives
February 2020
Categories
All
|