Bird
Raised Fist0
Unityframework~20 mins

Material properties in Unity - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does renderer.material.color control in Unity?
easy
A. The position of the object
B. The color of the object's material
C. The texture applied to the object
D. The size of the object

Solution

  1. Step 1: Understand the property name

    The property color inside renderer.material refers to the color attribute of the material.
  2. Step 2: Relate to object appearance

    Changing this property changes how the object looks in terms of color, not texture, position, or size.
  3. Final Answer:

    The color of the object's material -> Option B
  4. Quick Check:

    renderer.material.color changes color [OK]
Hint: Color property changes object color, not texture or size [OK]
Common Mistakes:
  • Confusing color with texture
  • Thinking it changes object position
  • Assuming it changes object scale
2. Which of the following is the correct way to set a material's color to red in Unity C#?
easy
A. renderer.material.color = Color.red;
B. renderer.material.color = "red";
C. renderer.material.color = new Color(255, 0, 0);
D. renderer.material.color = Color(1, 0, 0);

Solution

  1. Step 1: Check Unity color assignment syntax

    Unity uses predefined colors like Color.red or new Color(float r, float g, float b) with values between 0 and 1.
  2. Step 2: Evaluate each option

    renderer.material.color = Color.red; uses Color.red correctly. renderer.material.color = "red"; uses a string which is invalid. renderer.material.color = new Color(255, 0, 0); uses integers 255 which is incorrect for Color. renderer.material.color = Color(1, 0, 0); misses new keyword and uses wrong syntax.
  3. Final Answer:

    renderer.material.color = Color.red; -> Option A
  4. Quick Check:

    Use Color.red constant for red color [OK]
Hint: Use Color.red constant, not strings or 255 values [OK]
Common Mistakes:
  • Using string "red" instead of Color.red
  • Using 255 instead of 1 for color values
  • Missing 'new' keyword for Color constructor
3. What will be the output color of the object after running this code?
renderer.material.color = new Color(0.5f, 0.2f, 0.7f);
medium
A. Bright green
B. Black
C. Pure red
D. A shade of purple

Solution

  1. Step 1: Understand the Color constructor values

    The values represent red=0.5, green=0.2, blue=0.7 on a scale from 0 to 1.
  2. Step 2: Interpret the color mix

    Red and blue are medium to high, green is low, so the color will be a purple shade (mix of red and blue).
  3. Final Answer:

    A shade of purple -> Option D
  4. Quick Check:

    Color(0.5,0.2,0.7) = purple shade [OK]
Hint: Red + Blue mix with low green = purple shade [OK]
Common Mistakes:
  • Assuming values are 0-255 instead of 0-1
  • Confusing color channels order
  • Thinking low green means green color
4. Identify the error in this Unity C# code snippet that tries to change material shininess:
renderer.material.shininess = 0.8f;
medium
A. Material does not have a 'shininess' property directly accessible
B. The value 0.8f is out of range
C. Missing semicolon at the end
D. Should use renderer.material.color instead

Solution

  1. Step 1: Check if 'shininess' is a direct property

    Unity's standard material does not have a direct 'shininess' property; shininess is controlled via shader properties like '_Glossiness'.
  2. Step 2: Correct way to set shininess

    You must use renderer.material.SetFloat("_Glossiness", 0.8f); to change shininess.
  3. Final Answer:

    Material does not have a 'shininess' property directly accessible -> Option A
  4. Quick Check:

    Use SetFloat for shininess, no direct property [OK]
Hint: Use SetFloat("_Glossiness", value) for shininess [OK]
Common Mistakes:
  • Trying to access non-existent 'shininess' property
  • Confusing shininess with color
  • Ignoring shader property naming
5. You want to change the texture of a material at runtime in Unity. Which code snippet correctly does this assuming you have a Texture variable named newTexture?
hard
A. renderer.material.SetColor("_MainTex", newTexture);
B. renderer.material.color = newTexture;
C. renderer.material.mainTexture = newTexture;
D. renderer.material.texture = newTexture;

Solution

  1. Step 1: Identify the correct property for texture

    The main texture of a material is accessed via mainTexture property.
  2. Step 2: Check each option's validity

    renderer.material.mainTexture = newTexture; correctly assigns newTexture to mainTexture. renderer.material.color = newTexture; wrongly assigns texture to color. renderer.material.SetColor("_MainTex", newTexture); uses SetColor with a texture which is invalid. renderer.material.texture = newTexture; uses a non-existent 'texture' property.
  3. Final Answer:

    renderer.material.mainTexture = newTexture; -> Option C
  4. Quick Check:

    Use mainTexture to set material texture [OK]
Hint: Use mainTexture property to change textures [OK]
Common Mistakes:
  • Assigning texture to color property
  • Using SetColor instead of SetTexture
  • Using wrong property name 'texture'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard