Beginner Scripting: Scripts as behaviour components


This is the first coding task for semester 2. It was quick but very useful. I found it really interesting being able to change the colour of the object just by pressing the different keys corresponding to the colour. I feel like this could be useful going forward and thinking of different games to make. Maybe something along the lines of a child's game learning their colours. The script is for making these changes is below:


public class ExampleBehaviourScript : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.R)) { GetComponent<Renderer> ().material.color = Color.red; } if (Input.GetKeyDown(KeyCode.G)) { GetComponent<Renderer>().material.color = Color.green; } if (Input.GetKeyDown(KeyCode.B)) { GetComponent<Renderer>().material.color = Color.blue; } } }

Comments

Popular Posts