Properties and Understanding Variable Scope

Objective: Use properties to evaluate, get and set variables.

Natalia DaLomba
Women in Technology
5 min readJul 15, 2023

--

When using regular variables you have to keep track of your variables manually and create getter and setter methods. Also, if you want the variable to be accessible to other scripts, the user is able to set the variable and you may not want that.

What if you just want your variable to be read only and accessible to other scripts?

This is where properties come in handy!

You can think of properties as smart variables. Not only can you retrieve information from them, but you can also run functionality through them.

To better understand how properties can be visible, here are common types of variable scopes:

  • Local variable (in a method)
  • Instance field (belongs to an instance of a class)
  • Static field (One exists in the universe)

To create a property, you need to write the access modifier, data type and the name of the property. Properties can only be created as an instance field or a static field (as above).

Keep in mind the first letter of the property name should be capitalized, using pascal case. They also operate similar to methods and are evaluated every time the property is called. Syntax-wise, they just look different.

There’s various ways to write properties. So instead of writing the methods above GetSpeed and SetSpeed like this:

Properties manage themselves. You can write the property for Speed like so:

Your getter will almost always just be one line to return a value. For bools, you will not have a setter because wherever you’re setting the bool to be true or false will only be further down in your script, wherever certain logic is happening. It’s a reflection of the property’s state so don’t set the property in your code directly. An example of that would be:

So when Foobar() is called, IsSpeedValid will evaluate to true because speed is set to 5 and 5 is greater than 0.

It makes sense for variable types other than bools to have setters. When you have more code in your setter, typically it’ll be multiple lines. Here’s the longer and shorter ways to write IsSpeedValid’s getter:

Keep in mind writing it shorthand like that is only when you don’t have a setter.

Let’s look at another example. Below, we change the position of an object. We typically do this by accessing the transform and then the position property.

We’re able to set the position through the position property because as you can see with the intellisense, there’s a get and set option.

Another example of this is Time.deltaTime. Notice as you type this, the deltaTime property only has a get option. That means we can’t set this value which was a choice by the developer who wrote the deltaTime property.

Defining and Declaring our Own Properties

Here, we have a game state to check if the game is over using regular variables first.

This traditional isGameOver variable is public that can be accessed by any script and can be modified/set. Let’s say if isGameOver is true, we want to trigger a game over screen. A way we can achieve this, is when the player dies, we can call a method GameOver().

Basically, before the player object is destroyed, we will call GameOver().

This is an okay way to do this. The problem though, is we don’t have control over the isGameOver variable. We want to be able to read the value, but not necessarily set it.

We could create a method to return the value but that’s not practical. We will use a property. We want people to be able to read the value and not set it. Because it’s public, they don’t have to use the GameOver method we wrote — they can just change isGameOver directly with how it’s set up right now.

This is where properties come in. With a property, we don’t even need GameOver(). We can handle what the method was doing entirely in the property. Let’s change the public bool isGameOver to private and create the property.

The wonderful thing about properties is you can much more easily manage your code. If you have to make a change to the property, it changes how it’s evaluated in your entire code base instead of having to go through every point at which you used the variable.

--

--

Natalia DaLomba
Women in Technology

A Unity C# developer inspired by game design logic used to create digital adventures. https://www.starforce.games/devlog/