CTIN 483

Week 1 Thursday

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.

Demo: Light Switch

Turn a light on/off:

  • in the Unity editor
  • from a script
  • from a script based on keyboard input

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:

  1. Attach a script to the same GameObject as the Light.
  2. Observe that the Light is a Component.
  3. Figure out how to get a reference to a component.
  4. 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

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.

 

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.

Listening for keyboard input generally looks like this:

void Update () {
   if (Input.GetKeyDown (KeyCode.RightArrow)) {
      //do stuff
   }
}

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
   }
}

You have to give GetKeyDown some information, that is, what key it should check. Put that information in ()s when you call the function.

void Update () {
   if (Input.GetKeyDown (KeyCode.RightArrow)) {
      //do stuff
   }
}

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.

void Update () {
   if (Input.GetKeyDown (KeyCode.RightArrow)) {
      //do stuff
   }
}

GetKeyDown returns true or false depending on what the player did.

 

If the player just pressed the key, then GetKeyDown is true for exactly one frame. Then it is false until the player lets go of the key and presses it again.

So GetKeyDown tells you about an event (did the player just press the key) rather than a state (is the key down).

 

Here are the ways you can ask Unity about keyboard input:

GetKeyDown Did the player just press the key?
GetKey Is the key down right now?
GetKeyUp Did the player just release the key?

Here is what happens each frame with this code.

  1. Unity runs the GetKeyDown function.
  2. The function returns true or false.
  3. Unity uses the true or false value to determine whether it should run the code inside the braces.
void Update () {
   if (Input.GetKeyDown (KeyCode.RightArrow)) {
      //do stuff
   }
}

If you find it clearer, you can write it like this:

void Update () {
   if (Input.GetKeyDown (KeyCode.RightArrow) == true) {
      //do stuff
   }
}

The value of the function is already true or false, so you don't have to ask if true is true. But write it the way that makes sense to you.

CTIN 483 - W1 Th Aug 26 - 2021 Fall

By Margaret Moser

CTIN 483 - W1 Th Aug 26 - 2021 Fall

Review variables, types, and functions. Talking to other components. Keyboard input.

  • 241