0
0
Unityframework~20 mins

Image and raw image components in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.