From de73de8751cd2a0312e278a7c5ff1f52fc758ae0 Mon Sep 17 00:00:00 2001 From: Rick Date: Fri, 29 Jun 2018 16:45:19 +1000 Subject: [PATCH] 16 For Loops --- README.md | 15 ++++++++++++++- Text101/Assets/Scripts/AdventureGame.cs | 15 +++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ce7765d..d5546ae 100644 --- a/README.md +++ b/README.md @@ -225,4 +225,17 @@ Deeper understanding of the concepts covered thus far in this section. **After watching (learning outcomes)…** -Create a WebGL build and publish online. \ No newline at end of file +Create a WebGL build and publish online. + + +### 16 For Loops ### + +**In this video (objectives)…** + +1. Find a bug in our game which causes our standalone PC build to break. +2. Figure out why the game is breaking because of the bug. +3. Use a for loop to change our input mechanism and stop ArrayOutOfIndex errors. + +**After watching (learning outcomes)…** + +Fix our ArrayOutOfIndex error using a for loop for player input. \ No newline at end of file diff --git a/Text101/Assets/Scripts/AdventureGame.cs b/Text101/Assets/Scripts/AdventureGame.cs index 067d91e..b06871d 100644 --- a/Text101/Assets/Scripts/AdventureGame.cs +++ b/Text101/Assets/Scripts/AdventureGame.cs @@ -28,17 +28,12 @@ void Update() private void ManageState() { var nextStates = state.GetNextStates(); - if(Input.GetKeyDown(KeyCode.Alpha1)) + for (int index = 0; index < nextStates.Length; index++) { - state = nextStates[0]; - } - else if (Input.GetKeyDown(KeyCode.Alpha2)) - { - state = nextStates[1]; - } - else if (Input.GetKeyDown(KeyCode.Alpha3)) - { - state = nextStates[2]; + if (Input.GetKeyDown(KeyCode.Alpha1 + index)) + { + state = nextStates[index]; + } } textComponent.text = state.GetStateStory(); }