Time for action – adding keyboard interaction to scripts
Perform the following steps to add keyboard interaction to your scripts:
Add the following
if
statement to yourUpdate
function that checks if the Return key is pressed:// Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.Return)) { } }
Everything contained within the
if
statement will run every time theUpdate
function detects that the Return key has just been pressed down (there's a slightly different way to check if a key is being held down, but we'll cover that in the next chapter).We could add code directly inside the
if
statement to change the color of the text, but it's better to write a function for an action like that in case you want to replicate the same functionality outside of thatif
statement without writing redundant code, so we'll create a dedicatedChangeColor
function.Declare the
ChangeColor
function below theUpdate
function in yourColorChanger
script, as shown in the following code...