CTIN 583
Week 1 Wednesday
Exercise:
Write compiler errors. Paste them here.
Demo: Light Switch
Turn a light on/off:
- in the Unity editor
- from a script
- from a script based on keyboard input
Variables: Review
Variables: Type
In C#, when we want to remember something we have to tell the computer what kind of value it is:
string x = "Shigeru-san"
bool y = false
The technical word for this is type.
Variables: Names
Use meaningful variable names:
string firstName = "Percival"
bool hasATaco = false
int noOfPlayers = 4
float rotationSpeed = 3.14f
The f is important!
Variables: Conventions
By convention, variables in C# always begin with a lowercase letter.
string firstName = "Shigeru-san"
string FirstName = "Shigeru-san"
The compiler will not complain if you use an uppercase letter, but you will prevent a lot of bugs if you keep it consistent.
Variables: Unity Types
Working with Unity we'll have access to lots of more interesting kinds of variables:
GameObject player
Light spotlight
Transform parent
Camera minimapCamera
AudioClip hurraySound
Learning how to work with these in code is most of what it is to learn Unity.
Variables: Objects
These variables are objects, so they can have their own "variables", called properties.
GameObject player
Light spotlight
Transform myBase
string player.name
bool spotlight.enabled
Vector3 myBase.position
For example, player is a GameObject. It has a property called name that is a string.
Variables: Objects
Sometimes those properties are themselves objects! It's like a set of nesting dolls.
Transform myBase
Vector3 myBase.position
float myBase.position.x
Here, the Transform has a property that is a Vector3, and the Vector3 in turn has a float called x. And it can get deeper!
Variables: Exercise
This is a Text component, which we'll be working with very soon. Can you find properties that are:
- a string
- a bool
- an int
- a float?

Variables: Exercise
Search online for
"2019 Unity API text".
Open the first result and look under Properties (at the top).

Variables: Exercise

Try to find a couple of properties you see in the image, e.g. alignment or font.
Click on each one in the API page and look at the top of the page to see their type.

Variables in Unity Components
There are several types that you probably won't understand yet, for example this Unity-defined enumeration.
But now you know how to get started working with the properties of a component.

Functions: Review
also known as Methods
Functions
A function or method is a chunk of code with a name.
The simplest version looks like this:
void HandlePlayerDeath() {
//show animation, play sad music,
//animate camera, etc.
}
You call a function (tell it to run its code) like this:
//somewhere else in your code
HandlePlayerDeath();
Functions: Conventions
By convention, functions in C# always begin with an uppercase letter.
The compiler will not complain if you use an lowercase letter, but you will prevent a lot of bugs if you keep it consistent.
void HandlePlayerDeath() {}
void handlePlayerDeath() {}
Functions: Parameters
Some functions need information to do their job. That information is called a parameter, and it looks like this:
void HandleDamage(int damage) {
health = health - damage;
}
Inside the function you will have access to the information using the name of the parameter, much like you would use a variable.
Functions: Parameters
When you call a function that expects a parameter, it looks like this:
void HandleDamage(int damage) {
health = health - damage;
}
//somewhere else in your code
HandleDamage(20);
Functions: Return Type
Some functions give information back: they return a value. The value has to have a type, just like every other value in C#.
Most functions we'll see return nothing.
void HandleDamage(int damage) {
}
void Update() {
}
Functions: Return Type
But when a function does return a value it looks like this:
int CalculateComboScore(int score, int bonus) {
return score * bonus;
}
//somewhere else in your code
comboTotal =
CalculateComboScore(matchValue, chainBonus);
Talking to (Other) Components
The Manual Way
In Unity, as we saw last time, you can turn a component on or off using this checkmark:

But you won't be there to turn it on and off when someone is playing your game. How can you get to this checkbox from code?

Here are the first steps to address the Light from your code:
- Attach a script to the same GameObject as the Light.
- Observe that the Light is a Component.
- Figure out how to get a reference to a component.
- Determine what name Unity uses for the checkbox from code.
Components are Types
Each component is a class. Every class is a type of variable. So when you want to store a component in a variable, it looks like this:
Transform myTransform
Light theSpotlight
AudioSource audio
Rigidbody2D rb2d
The C# MonoBehaviours you write yourself are also Components:
PlayerMovement movement
Goomba theGoomba
StateManager stateManager
ScoreUI score
We'll learn more about how to work with other scripts later.
GetComponent
GetComponent is an important and powerful bit of code, because it lets you talk to any of the components you can see in the Inspector (including other scripts).
So it is worth all the punctuation!
myLight = gameObject.GetComponent<Light> ();
GetComponent is how you can talk to other components from a script. For example, here's how you can get a reference to a Light component:
GetComponent
myLight = gameObject.GetComponent<Light> ();
How do you know what to put in the <>s? It's almost always the same as the name of the component that you see in the Inspector.

Remember that gameObject refers to the GO that your script is attached to. Later we will see how to talk to other GOs.
Component References
A lot of what you do when you're working with Unity is talking to components. To do this you need a reference – a variable that points to the component you want.
Rigidbody rb; Collider myCollider; void Start() { rb = gameObject.GetComponent<Rigidbody>(); myCollider = gameObject.GetComponent<Collider>(); }
Component References
So we will often declare a variable with the type of component we want, and then go find it in the Start function.
Rigidbody rb; Collider myCollider; void Start() { rb = gameObject.GetComponent<Rigidbody>(); myCollider = gameObject.GetComponent<Collider>(); }
We'll see this pattern over and over in the scripts we write in class.
Working with Components
So you've got a Light. What can you do with it?
You can talk to its properties, such as color. You can also get to the checkbox that turns it on and off.

Working with Components
The checkbox here is called "enabled" from code. Nearly every component can be turned on and off this way using code like this:
myLight.enabled = false;

Working with Components
You can nearly always figure out how to talk to the property you care about by looking at its name in the Inspector.

myLight.color = Color.green; myLight.intensity = 1.2f;
"Color"
"Intensity"
Working with Components
But Unity sometimes gets creative. You can use the little book icon to get to the Unity documentation and sort out what properties are called.

myLight.lightmapBakeType = LightmapBakeType.Realtime;
myLight.bounceIntensity = 0.8f;
myLight.shadows = LightShadows.Soft;
"Mode"
"Indirect Multiplier"
"Shadow Type"
Shortcut to Doc
Every component is documented, with its methods and properties, in the Unity Scripting API.
As a shortcut you can click the question mark, then click the button that says "Switch to Scripting".


Keyboard Input
Listening for keyboard input generally looks like this:
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow)) { //do stuff } }
Keyboard Input
Keyboard Input
Each time through Unity's loop there is an Input phase where it checks what the player is doing. It stores all the information in the Input object.
Our Update code runs during the Game Logic phase of the process. So we can ask the Input object what happened and know we are getting the latest information.
The Input object that Unity provides has a function called GetKeyDown to ask whether the player just pressed a particular key on the keyboard.
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow)) { //do stuff } }
Keyboard Input
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow)) { //do stuff } }
Keyboard Input
We have to give GetKeyDown some information, that is, what key it should check. We put that information in ()s when we call the function.
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow)) { //do stuff } }
Keyboard Input
Unity provides a (very) long list of keys that you might care about in the KeyCode object.
To see the whole list, you can type Keycode. (note the period) and wait.
GetKeyDown returns true or false depending on what the player did.
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow)) { //do stuff } }
Keyboard Input
Here I am feeding the result directly into the if condition, which happens to want a value that is either true or false.
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow)) { //do stuff } }
Keyboard Input
If you find it clearer, you can write this out:
void Update () { if (Input.GetKeyDown (KeyCode.RightArrow) == true) { //do stuff } }
Keyboard Input
CTIN 583 - W1 Wed Jan 12 - 2022 Spring
By Margaret Moser
CTIN 583 - W1 Wed Jan 12 - 2022 Spring
Review variables, types, and functions. Talking to other components. Keyboard input.
- 228