Bird
Raised Fist0
Unityframework~20 mins

Materials and textures in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Master of Materials and Textures
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 when applying a texture?

Consider the following Unity C# script snippet that assigns a texture to a material. What will be the color of the material's main texture pixel at (0,0) after this code runs?

Unity
Texture2D tex = new Texture2D(2, 2);
tex.SetPixel(0, 0, Color.red);
tex.SetPixel(1, 0, Color.green);
tex.SetPixel(0, 1, Color.blue);
tex.SetPixel(1, 1, Color.white);
tex.Apply();

Material mat = new Material(Shader.Find("Standard"));
mat.mainTexture = tex;

Color pixelColor = ((Texture2D)mat.mainTexture).GetPixel(0, 0);
Debug.Log(pixelColor);
ARGBA(1.000, 0.000, 0.000, 1.000)
BRGBA(0.000, 0.000, 1.000, 1.000)
CRGBA(0.000, 1.000, 0.000, 1.000)
DRGBA(1.000, 1.000, 1.000, 1.000)
Attempts:
2 left
💡 Hint

Remember that SetPixel sets the color at the specified coordinates before Apply() updates the texture.

🧠 Conceptual
intermediate
1:30remaining
Which Unity shader property controls the smoothness of a material?

In Unity's Standard Shader, which property name is used to control how smooth or shiny a material appears?

A"_Emission"
B"_Metallic"
C"_BumpMap"
D"_Glossiness"
Attempts:
2 left
💡 Hint

Think about the term that relates to surface reflection sharpness.

🔧 Debug
advanced
2:30remaining
Why does this texture not appear on the material in Unity?

Look at this code snippet. The texture is created and assigned to the material, but the object remains untextured in the scene. What is the most likely cause?

Unity
Texture2D tex = new Texture2D(128, 128);
// Forgot to call tex.Apply()
for (int y = 0; y < 128; y++) {
    for (int x = 0; x < 128; x++) {
        tex.SetPixel(x, y, Color.yellow);
    }
}
Material mat = new Material(Shader.Find("Standard"));
mat.mainTexture = tex;
AThe material was not assigned to any renderer component.
BThe texture size is too large and causes a memory overflow.
CThe texture was never applied with tex.Apply(), so changes are not updated.
DThe shader "Standard" does not support textures.
Attempts:
2 left
💡 Hint

Remember that Texture2D changes require a specific method call to update.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a tiled texture offset in Unity C#?

You want to tile a texture twice in both X and Y directions and offset it by 0.5 on X axis. Which code snippet correctly sets this on a material?

A
mat.mainTextureScale = new Vector2(2, 2);
mat.mainTextureOffset = new Vector2(0.5f, 0);
B
mat.SetTextureScale("_MainTex", new Vector2(2, 2));
mat.SetTextureOffset("_MainTex", new Vector2(0.5f, 0));
C
mat.mainTextureScale = new Vector2(0.5f, 0);
mat.mainTextureOffset = new Vector2(2, 2);
D
mat.SetTextureScale("_MainTex", new Vector2(0.5f, 0));
mat.SetTextureOffset("_MainTex", new Vector2(2, 2));
Attempts:
2 left
💡 Hint

Remember that mainTextureScale and mainTextureOffset are properties, but SetTextureScale and SetTextureOffset require the texture property name.

🚀 Application
expert
3:00remaining
How many unique colors will this procedural texture contain?

This Unity C# code creates a 4x4 texture where each pixel color depends on its coordinates. How many unique colors does the resulting texture have?

Unity
Texture2D tex = new Texture2D(4, 4);
for (int y = 0; y < 4; y++) {
    for (int x = 0; x < 4; x++) {
        Color c = (x == y) ? Color.black : Color.white;
        tex.SetPixel(x, y, c);
    }
}
tex.Apply();
A2
B4
C8
D16
Attempts:
2 left
💡 Hint

Think about how many pixels are black and how many are white.

Practice

(1/5)
1. What does a Material in Unity primarily control?
easy
A. The object's position in the scene
B. The shape of the object
C. The physics behavior of the object
D. The color and surface appearance of an object

Solution

  1. Step 1: Understand the role of materials

    Materials define how an object looks by controlling its color and surface properties like shininess or transparency.
  2. Step 2: Differentiate from other components

    Shape is controlled by meshes, physics by Rigidbody, and position by Transform, not materials.
  3. Final Answer:

    The color and surface appearance of an object -> Option D
  4. Quick Check:

    Material = color and surface [OK]
Hint: Materials control look, not shape or physics [OK]
Common Mistakes:
  • Confusing material with mesh or physics
  • Thinking material changes object position
  • Mixing material with texture only
2. Which of the following is the correct way to assign a material to a GameObject's Renderer in C#?
easy
A. gameObject.SetMaterial(newMaterial);
B. gameObject.renderer.material = newMaterial;
C. gameObject.GetComponent<Renderer>().material = newMaterial;
D. gameObject.material = newMaterial;

Solution

  1. Step 1: Identify the correct component access

    In Unity C#, you access the Renderer component using GetComponent<Renderer>().
  2. Step 2: Assign the material property correctly

    The material is assigned via the material property of the Renderer component.
  3. Final Answer:

    gameObject.GetComponent<Renderer>().material = newMaterial; -> Option C
  4. Quick Check:

    Use GetComponent<Renderer>() to assign material [OK]
Hint: Use GetComponent<Renderer>() to access material [OK]
Common Mistakes:
  • Using deprecated 'renderer' shortcut
  • Calling non-existent SetMaterial method
  • Assigning material directly to GameObject
3. What will be the output of this code snippet in Unity C#?
Renderer rend = gameObject.GetComponent<Renderer>();
Texture2D tex = new Texture2D(128, 128);
rend.material.mainTexture = tex;
Debug.Log(rend.material.mainTexture.width);
medium
A. 128
B. 0
C. null
D. Runtime error

Solution

  1. Step 1: Understand texture creation

    A new Texture2D of size 128x128 is created and assigned to the material's mainTexture.
  2. Step 2: Access texture width property

    Since the texture is valid and assigned, accessing mainTexture.width returns 128.
  3. Final Answer:

    128 -> Option A
  4. Quick Check:

    Texture width = 128 [OK]
Hint: New Texture2D size sets width property [OK]
Common Mistakes:
  • Assuming texture is null before assignment
  • Confusing texture size with pixel data
  • Expecting runtime error from assignment
4. Identify the error in this Unity C# code that tries to apply a texture to a material:
Renderer rend = GetComponent<Renderer>();
Texture2D tex;
rend.material.mainTexture = tex;
medium
A. Texture2D tex is declared but not initialized
B. Renderer component is not accessed correctly
C. mainTexture property cannot be assigned
D. GetComponent<Renderer>() should be called on gameObject

Solution

  1. Step 1: Check texture initialization

    The variable tex is declared but never assigned a texture object, so it is null.
  2. Step 2: Understand assignment consequences

    Assigning null to mainTexture will remove the texture, likely not intended and may cause issues.
  3. Final Answer:

    Texture2D tex is declared but not initialized -> Option A
  4. Quick Check:

    Uninitialized texture = null assignment error [OK]
Hint: Always initialize textures before assignment [OK]
Common Mistakes:
  • Forgetting to create or load the texture
  • Assuming GetComponent works without gameObject
  • Thinking mainTexture is read-only
5. You want to create a material that uses a texture only on the top face of a cube in Unity. Which approach is best?
hard
A. Create multiple materials and assign one to the whole cube
B. Use a custom shader with UV mapping to apply texture only on the top face
C. Assign the texture to the material's mainTexture and it will auto-apply to top face
D. Change the cube's color to match the texture color on the top face

Solution

  1. Step 1: Understand texture application on specific faces

    Unity materials apply textures based on UV mapping. To target only the top face, UVs or shader logic must isolate that face.
  2. Step 2: Evaluate options for selective texturing

    Assigning texture to mainTexture applies it to all faces. Multiple materials can assign different materials per face but require mesh setup. Changing color won't apply texture.
  3. Step 3: Choose best approach

    Using a custom shader with UV mapping allows precise control to show texture only on the top face.
  4. Final Answer:

    Use a custom shader with UV mapping to apply texture only on the top face -> Option B
  5. Quick Check:

    Custom shader + UV mapping = selective texture [OK]
Hint: Use UV mapping in shader for face-specific textures [OK]
Common Mistakes:
  • Assuming mainTexture auto-applies per face
  • Ignoring mesh UV layout
  • Trying to recolor instead of texturing