Why You Need to Get, Change and Set Structs

Objective: Understand why and how to get, change and set structs.

Natalia DaLomba
Women in Technology
3 min readJul 5, 2023

--

Structs are a type of variable that need to be handled differently. Some examples are Vector3 and Color. In this demonstration, we will be decreasing an object’s alpha channel by 20% every time we press the Space key.

Initially, we create a game object reference for us to drag in our sphere object we want to manipulate. Then we make a renderer variable and set it to the sphere’s Renderer in Start.

[SerializeField] private GameObject sphere;
private Renderer renderer;

void Start() {
renderer = sphere.GetComponent<Renderer>();
}

Next, we write the DecreaseObjectAlpha method which will do exactly what the method name describes. We pass in a float value called increment which we will set as 0.2 to decrease the alpha by 20%.

public void DecreaseObjectAlpha(float increment) {
Color c = renderer.material.color;
c.a -= increment;
renderer.material.color = c;
}

The goal is, you are trying to make a material a bit more transparent. The way to do that is generally by using the material’s main color. A material can have any number of properties on it to do anything you want.

The shader determines how all the material properties are used. Generally, “_Color” is the main color property in shaders in Unity. This is abbreviated for us by the “.color” C# property so you don’t even have to care that it’s called “_Color” in the shader code.

So to make the material a bit more transparent, you want to change the material’s main color. Specifically, you want to change that color’s alpha channel, because you know that the shader is going to use alpha in this case for transparency.

The reason you are “getting, changing, and setting” is because renderer.material.color is a C# property that returns a Color. Color is a struct, so it’s a Value Type, and is copied by value (deep cloned) back to you, the caller. You can’t modify that value, it’d be a compiler error.

They could have let you do it, but the language creators decided to not let you do it, so you see your logic error sooner. This is so you don’t spend 4 days trying to find why this code snippet below does nothing:

renderer.material.color.a -= 0.2f;

Get a copy of the material’s main color, set its alpha to 0.2 less, and throw it out. That’s what it’d be doing, so C# decided to make it a compiler error. Our approach is by getting, changing, and setting. Let’s break down what we did above.

Get

Color c = renderer.material.color; Create a local Color variable c that is a copy of the sphere’s renderer’s material’s color. The key is we’re temporarily storing a copy of the material’s color in the c variable. Then we will later reassign c to the sphere’s entire color, setting the alpha in the process.

Change

c.a -= incrementer; c.a is accessing the color’s alpha channel. increment is being subtracted from c’s alpha, which in our case will be 20% each time the Space key is pressed.

Set

renderer.material.color = c; Finally, we will assign our temporary c variable with the changes we’d like to make back to our color on our sphere.

Lastly, we need to call our DecreaseObjectAlpha method in Update every time the Space key is pressed. We pass in the 20% here, or in code terms, it’s .2f:

void Update() {
if (Input.GetKeyDown(KeyCode.Space))
DecreaseObjectAlpha(.2f);
}

Jump into Unity and go under the sphere’s material. Make sure the Rendering Mode is set to Fade to fully reflect the changes in the Scene view and Game view.

--

--

Natalia DaLomba
Women in Technology

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