Intro to 3D Game Development

0 of 193 lessons complete (0%)

Scripting Part 3: Finishing the Game Loop

Use a For Loop to Remove Treasure from All the Dig Spots

You don’t have access to this lesson
Please register or sign in to access the course content.

Next we will need to reset all of the dig spots. We can repeat code on each element of an array easily using a for… of loop. This is a shortcut version of a classic for loop that works especially well for this purpose. 

  1. In the TreasureControllerScript, find the randomizeTreasure function, and create a for loop using the keyword for, ( ) parentheses, and { } curly brackets. This should look a lot like an if-statement or a function. 
  2. Between the ( ) parentheses, create a new variable called spot with let, and use the keyword of to say that it will be each of the elements of the digSpots array.
  3. In a new line between the { } curly brackets, send the event “setHasTreasure” with the value false to the spot variable. 
  4. Finally, call the randomizeTreasure function at the end of your resetGame function.