0
0
Unityframework~20 mins

Material properties in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Material Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code changing material color?

Consider this Unity C# script snippet that changes a material's color in the Start() method. What color will the material have after running?

Unity
using UnityEngine;

public class ColorChanger : MonoBehaviour {
    void Start() {
        Renderer rend = GetComponent<Renderer>();
        rend.material.color = Color.red;
        rend.material.color = new Color(0, 1, 0, 1); // green
    }
}
AThe material color will be green.
BThe material color will be red.
CThe material color will be blue.
DThe material color will be white.
Attempts:
2 left
💡 Hint

Think about the order of color assignments.

Predict Output
intermediate
2:00remaining
What does this shader property code print?

This Unity C# code tries to read a float property from a material. What will it print?

Unity
using UnityEngine;

public class PropertyReader : MonoBehaviour {
    public Material mat;
    void Start() {
        if (mat.HasProperty("_Glossiness")) {
            float gloss = mat.GetFloat("_Glossiness");
            Debug.Log(gloss);
        } else {
            Debug.Log("Property not found");
        }
    }
}
AIt prints "Property not found".
BIt throws a NullReferenceException.
CIt prints the glossiness value as a float (e.g., 0.5).
DIt prints 0 always.
Attempts:
2 left
💡 Hint

Check if the material has the property before reading it.

🔧 Debug
advanced
2:00remaining
Why does this code fail to change the material color?

This Unity C# script tries to change the color of a material but the color does not update in the game. Why?

Unity
using UnityEngine;

public class ColorFail : MonoBehaviour {
    public Material mat;
    void Start() {
        mat.color = Color.blue;
    }
}
AThe material is null and causes a runtime error.
BThe material is shared and changes do not affect the renderer's instance.
CThe script is missing a call to <code>mat.Apply()</code> to update the material.
DThe color property name is incorrect; it should be "_Color".
Attempts:
2 left
💡 Hint

Think about how Unity handles shared materials vs instances.

📝 Syntax
advanced
2:00remaining
Which option correctly sets a texture property on a material?

Choose the correct Unity C# code to set a texture named "_MainTex" on a material.

Amat.SetTexture("_MainTex", myTexture);
Bmat.SetTexture(myTexture, "_MainTex");
Cmat.SetTexture("MainTex", myTexture);
Dmat.SetTexture("_mainTex", myTexture);
Attempts:
2 left
💡 Hint

Check the method signature and property name case sensitivity.

🚀 Application
expert
3:00remaining
How to smoothly change a material's emission color over time?

You want to make a material's emission color fade from black to red smoothly over 3 seconds in Unity. Which code snippet achieves this?

A
void Update() {
    mat.SetColor("_EmissionColor", Color.Lerp(Color.red, Color.black, Time.time / 3f));
}
B
void Update() {
    mat.SetColor("_EmissionColor", Color.red * Time.time / 3f);
}
C
void Start() {
    mat.SetColor("_EmissionColor", Color.red);
    yield return new WaitForSeconds(3);
    mat.SetColor("_EmissionColor", Color.black);
}
D
IEnumerator FadeEmission() {
    float duration = 3f;
    float elapsed = 0f;
    while (elapsed &lt; duration) {
        float t = elapsed / duration;
        mat.SetColor("_EmissionColor", Color.Lerp(Color.black, Color.red, t));
        elapsed += Time.deltaTime;
        yield return null;
    }
}
Attempts:
2 left
💡 Hint

Think about using a coroutine and Color.Lerp with elapsed time.