Presentation is loading. Please wait.

Presentation is loading. Please wait.

UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

Similar presentations


Presentation on theme: "UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D."— Presentation transcript:

1 UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D

2 UFCFS D Technologies for the Web Agenda Why Use an Array Array Declaration in C# in Unity 3D Searching and Sorting an Array Parallel Arrays Arrays of Data Array Access Errors

3 UFCFS D Technologies for the Web Need to Store Five Numbers Use individual variables for each number stored: int numberOne; int numberTwo; int numberThree; int numberFour; int numberFive; messy - have to process as individual items rather than as a collection of grouped items. Solution - use an Array - contiguous data storage

4 UFCFS D Technologies for the Web Arrays and Data Storage Elements The value stored at array element number 2 is 14 Arrays store values in ‘elements’ (compartments) i.e. contiguous memory locations

5 UFCFS D Technologies for the Web Arrays and Data Storage Elements The array has 5 elements 0..4 The value stored at array element number 4 is 23 Array element numbering starts at Thus the size of the array is 5

6 UFCFS D Technologies for the Web Arrays and Data Types An Array of numbers (integers) Gibson Fender Aria Martin Array of strings Les Paul

7 UFCFS D Technologies for the Web Declaring Arrays in C# // create a new array int[] numberArray = new int[5]; // store the numbers into the array // numberArray[0] = 15; numberArray[1] = 95; numberArray[2] = 14; numberArray[3] = 70; numberArray[4] = 23;

8 UFCFS D Technologies for the Web Array Declaration and Assignment in Unity 3d String[] nameArray = new string[5]; void Start () { nameArray[0] = "Hello"; nameArray[1] = "from"; nameArray[2] = "the"; nameArray[3] = "Name"; nameArray[4] = "Array"; }

9 UFCFS D Technologies for the Web Accessing Array Elements in Unity 3d // iterate through the array for (int index = 0; index <= 4; index++) { Debug.Log(nameArray[index]); }

10 UFCFS D Technologies for the Web Typical Aspects of using Arrays Search an array for a given value Sort an array in a given order Find the size of the array Use the length method in searching algorithms Compare the contents of one array with another array

11 UFCFS D Technologies for the Web // total the numbers stored in the number array int index; int sum; sum = 0; for (index = 0; index <= 4; index = index + 1){ sum = sum + numberArray[index] } Debug.Log("The sum of the numbers in the array is " + sum) Summing the Values of an Array

12 UFCFS D Technologies for the Web Summing the Values of an Array 2 // total the numbers stored in the number array using array // length method int index; int sum; sum = 0; for (index = 0; index <= numberArray.length; index++){ sum = sum + numberArray[index] } Debug.Log("The sum of the numbers in the array is " + sum)

13 UFCFS D Technologies for the Web Parallel Arrays name John Ben Freda Sue Mike

14 UFCFS D Technologies for the Web Parallel Array Search name John Ben Freda Sue Mike Search on name Extract on name

15 UFCFS D Technologies for the Web Parallel Array Search name John Ben Freda Sue Mike Search on name Find occurrences on name of

16 UFCFS D Technologies for the Web Arrays of Data (sounds) Array of.mp3 audio files footsteps.mp3 shell.mp3 siren.mp3 alarm.mp3 ambience.mp3

17 UFCFS D Technologies for the Web Array of Data (sounds) footsteps.mp3 shell.mp3 siren.mp3 alarm.mp3 ambience.mp3 Use code to produce random sound effect in 3D environment

18 UFCFS D Technologies for the Web Example of Accessing Random Array Elements in Unity 3d void Update () { // use Random.Range function that matches array size getRandomName = Random.Range(0.0, 4.0); // convert the decimal random value to an integer index = Mathf.RoundToInt(getRandomName); // use the value to access a random array element Debug.Log(nameArray[index]); }

19 UFCFS D Technologies for the Web Beware! The classic runtime error is where a program tries to access an array element beyond the array’s size. getRandomName = Random.Range(0.0, 5.0); The array has only five elements, but the code is attempting to access an element number six, which does not exist. The computer speak is the code has ‘fallen off the end of the array’

20 UFCFS D Technologies for the Web Summary Arrays are a fundamental structured data type in any programming language Arrays store values usually of the same type in a contiguous way which allows the data to be accessed and manipulated as a collection of data Typical array access is to search and sort the contents Associated data may be held in parallel arrays and use cross referencing to extract the required information Algorithms that manipulate arrays must stay within the array bounds or runtime errors will occur causing the program to ‘freeze’ or ‘crash’.


Download ppt "UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D."

Similar presentations


Ads by Google