Bird
Raised Fist0
Unityframework~20 mins

Image and raw image components 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
🎖️
Image and RawImage 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 snippet?

Consider the following Unity C# code that sets a sprite to an Image component and a texture to a RawImage component. What will be the result when this code runs?

Unity
using UnityEngine;
using UnityEngine.UI;

public class TestImage : MonoBehaviour {
    public Image imageComponent;
    public RawImage rawImageComponent;
    public Sprite sprite;
    public Texture2D texture;

    void Start() {
        imageComponent.sprite = sprite;
        rawImageComponent.texture = texture;
        Debug.Log(imageComponent.sprite.name);
        Debug.Log(rawImageComponent.texture.name);
    }
}
AThrows an error because RawImage cannot use Texture2D.
BThrows a NullReferenceException because sprite is not assigned.
CLogs the texture's name twice, ignoring the sprite.
DLogs the sprite's name and the texture's name correctly.
Attempts:
2 left
💡 Hint

Remember that Image uses Sprite and RawImage uses Texture.

🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes the difference between Image and RawImage components in Unity?

Choose the correct description of how Image and RawImage components differ in Unity UI.

AImage displays Textures and supports slicing; RawImage displays Sprites and supports slicing.
BImage displays Sprites and supports slicing; RawImage displays Textures and does not support slicing.
CBoth Image and RawImage display Sprites and support slicing equally.
DRawImage displays Sprites and supports slicing; Image displays Textures and does not support slicing.
Attempts:
2 left
💡 Hint

Think about which component supports 9-slicing and which uses textures directly.

🔧 Debug
advanced
2:00remaining
What happens when this code runs?

Examine the code below. What best describes what happens at runtime?

Unity
using UnityEngine;
using UnityEngine.UI;

public class ImageTest : MonoBehaviour {
    public Image img;
    public Texture2D tex;

    void Start() {
        img.sprite = tex as Sprite;
    }
}
ACasting Texture2D to Sprite returns null, causing img.sprite to be null but no error.
BCasting Texture2D to Sprite fails, causing a runtime InvalidCastException.
Cimg.sprite cannot be assigned at runtime, causing an error.
DTexture2D cannot be assigned to img.sprite because they are incompatible types; no cast possible.
Attempts:
2 left
💡 Hint

Consider what happens when you cast incompatible types with 'as' keyword.

📝 Syntax
advanced
1:00remaining
Which option correctly sets a RawImage's texture in Unity C#?

Choose the correct syntax to assign a Texture2D named 'myTexture' to a RawImage component named 'rawImage'.

ArawImage.texture = myTexture;
BrawImage.texture = myTexture as Sprite;
CrawImage.SetTexture(myTexture);
DrawImage.sprite = myTexture;
Attempts:
2 left
💡 Hint

Remember RawImage uses a Texture, not a Sprite.

🚀 Application
expert
2:30remaining
How many items are in the resulting dictionary after this Unity C# code runs?

Given this code snippet that uses Image and RawImage components in a dictionary, how many key-value pairs does the dictionary contain after execution?

Unity
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class ImageDictTest : MonoBehaviour {
    public Image img1;
    public RawImage raw1;
    public Image img2;

    void Start() {
        var dict = new Dictionary<string, Object>();
        dict["first"] = img1.sprite;
        dict["second"] = raw1.texture;
        dict["first"] = img2.sprite;
        Debug.Log(dict.Count);
    }
}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Think about what happens when you assign a value to an existing key in a dictionary.

Practice

(1/5)
1. Which Unity UI component is best suited for displaying a sprite with color tinting?
easy
A. RawImage component
B. Image component
C. Text component
D. Button component

Solution

  1. Step 1: Understand the purpose of Image component

    The Image component is designed to display sprites and supports color tinting, making it ideal for sprite display with color changes.
  2. Step 2: Compare with RawImage component

    RawImage displays textures and allows UV adjustments but does not support sprite-specific features like tinting.
  3. Final Answer:

    Image component -> Option B
  4. Quick Check:

    Sprite + tinting = Image component [OK]
Hint: Image = sprites + tinting; RawImage = textures + UV [OK]
Common Mistakes:
  • Confusing RawImage with Image for sprites
  • Thinking Text or Button can display sprites
  • Ignoring color tinting feature
2. Which of the following is the correct way to assign a texture to a RawImage component in C# script?
easy
A. rawImage.texture = myTexture;
B. rawImage.sprite = myTexture;
C. rawImage.image = myTexture;
D. rawImage.setTexture(myTexture);

Solution

  1. Step 1: Recall RawImage property for texture

    RawImage uses the 'texture' property to assign a Texture object.
  2. Step 2: Check syntax correctness

    Assigning with 'rawImage.texture = myTexture;' is correct syntax in Unity C#.
  3. Final Answer:

    rawImage.texture = myTexture; -> Option A
  4. Quick Check:

    RawImage.texture = Texture [OK]
Hint: RawImage uses .texture, Image uses .sprite [OK]
Common Mistakes:
  • Using .sprite with RawImage
  • Calling non-existent methods like setTexture()
  • Confusing Image and RawImage properties
3. Given this code snippet in Unity C#:
Image img = GetComponent<Image>();
img.color = new Color(1, 0, 0, 0.5f);
What will be the visible effect on the Image component?
medium
A. The image will become fully transparent
B. The image will turn solid red with no transparency
C. The image color will not change
D. The image will have a semi-transparent red tint

Solution

  1. Step 1: Analyze the color assignment

    The color is set to red (1,0,0) with alpha 0.5, meaning 50% transparency.
  2. Step 2: Understand Image color effect

    Image component applies color tint including alpha, so the image will appear red and semi-transparent.
  3. Final Answer:

    The image will have a semi-transparent red tint -> Option D
  4. Quick Check:

    Color RGBA(1,0,0,0.5) = semi-transparent red [OK]
Hint: Alpha < 1 means transparency; RGB sets tint color [OK]
Common Mistakes:
  • Assuming alpha 0.5 means fully transparent
  • Thinking color change won't affect Image
  • Confusing solid color with tint
4. What is wrong with this code snippet that tries to assign a sprite to a RawImage component?
RawImage rawImg = GetComponent<RawImage>();
rawImg.sprite = mySprite;
medium
A. The sprite must be converted to a texture first
B. You must use 'rawImg.texture' to assign a sprite
C. RawImage does not have a 'sprite' property
D. The code should use 'rawImg.image' instead of 'rawImg.sprite'

Solution

  1. Step 1: Check RawImage properties

    RawImage component uses 'texture' property, not 'sprite'. 'sprite' is for Image component.
  2. Step 2: Understand property mismatch

    Assigning 'sprite' to RawImage causes error because it doesn't exist.
  3. Final Answer:

    RawImage does not have a 'sprite' property -> Option C
  4. Quick Check:

    RawImage lacks sprite property [OK]
Hint: RawImage uses .texture, not .sprite [OK]
Common Mistakes:
  • Trying to assign sprite directly to RawImage
  • Confusing Image and RawImage components
  • Assuming RawImage has 'image' property
5. You want to display a UI element in Unity that shows a texture with custom UV coordinates to create a scrolling effect. Which component should you use and how do you adjust the UVs?
hard
A. Use RawImage component and modify its uvRect property
B. Use Image component and change its sprite's UVs directly
C. Use RawImage component and change the color property
D. Use Image component and animate its color alpha

Solution

  1. Step 1: Identify component for texture with UV control

    RawImage supports textures and allows UV adjustments via uvRect property.
  2. Step 2: Understand how to create scrolling effect

    Changing uvRect offsets scrolls the texture on RawImage, enabling the effect.
  3. Final Answer:

    Use RawImage component and modify its uvRect property -> Option A
  4. Quick Check:

    Scrolling texture = RawImage + uvRect [OK]
Hint: Scroll textures by changing RawImage.uvRect [OK]
Common Mistakes:
  • Trying to modify sprite UVs in Image component
  • Using color changes to scroll texture
  • Confusing Image and RawImage for UV control