// Use this for initialization void Start() { obstacles = new Obstacle[ObstacleCount]; //creates a new array of the size requested for (int i = 0; i < ObstacleCount; i++) { obstacles [i] = new Obstacle(PrimitiveType.Sphere, Obstacle.MovementType.Static); treadMillUpdates += new UpdateObstacles(obstacles [i].DrawObstacle); } }
UpdateObstacles treadMillUpdates; // the delegate to assign functions is instantiated - UpdateObstacles is the type and treadMillUpdates is the identifier void Start() { obstacles = new Obstacle[ObstacleCount]; // creates a new array of the size requested for (int i = 0; i < ObstacleCount; i++) { obstacles [i] = new Obstacle(PrimitiveType.Sphere, Obstacle.MovementType.Static); // fills in the array with new obstacles, can choose a different primitive treadMillUpdates += new UpdateObstacles(obstacles[i].DrawObstacle); // by using += we stack the obstacles[i].DrawObstacle function into the treadMillUpdates delegate function. // At the end of the for loop, the treadMillUpdates() becomes a single function that calls a stack of other functions. // To use the delegate it's added to the Update() loop as a single statement. // assign each object's update to the delegate here // This code above allows us to use a single delegate function to call many other functions rather than using a for loop to iterate through each object's function in an array. } }
UpdateObstacles treadMillUpdates; // the delegate to assing functions void Start() { obstacles = new Obstacle[ObstacleCount]; // create a new array of the zize required for (int i = 0; i < ObstacleCount; i++) { obstacles[i] = new Obstacle(PrimitiveType.Sphere, Obstacle.MoveMentType.Static); //obstacles[i].InitObstacle(); // extra function call // fills in the array with new obstacles //assign each objects udpate to the delegate here treadMillUpdates += new UpdateObstacles(obstacles[i].DrawObstacle); } }