Game Development Reference
In-Depth Information
You can also build the array manually in code via the Start function instead of
using the Object Inspector. This ensures that the array is constructed as the level
begins. Either method works fine, as shown in the following code sample 1-5:
01 using UnityEngine;
02 using System.Collections;
03
04 public class MyScriptFile : MonoBehaviour
05 {
06 //Array of game objects in the scene
07 public GameObject[] MyObjects;
08
09 // Use this for initialization
10 void Start ()
11 {
12 //Build the array manually in code
13 MyObjects = new GameObject[3];
14 //Scene must have a camera tagged as MainCamera
15 MyObjects[0] = Camera.main.gameObject;
16 //Use GameObject.Find function to
17 //find objects in scene by name
18 MyObjects[1] = GameObject.Find("Cube");
19 MyObjects[2] = GameObject.Find("Cylinder");
20 }
21
22 // Update is called once per frame
23 void Update ()
24 {
25 }
26 }
The following are the comments for code sample 1-5:
Line 10 : The Start function is executed at level startup. Functions are
considered in more depth later in this chapter.
Line 13 : The new keyword is used to create a new array with a capacity
of three. This means that the list can hold no more than three elements at
any one time. By default, all elements are set to the starting value of null
(meaning nothing). They are empty.
 
Search WWH ::




Custom Search