0
0
Unityframework~10 mins

Image and raw image components in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the Image component from the GameObject.

Unity
Image img = gameObject.GetComponent<[1]>();
Drag options to blanks, or click blank then click option'
ARawImage
BImage
CTexture
DSpriteRenderer
Attempts:
3 left
💡 Hint
Common Mistakes
Using RawImage instead of Image when you want the Image component.
Using SpriteRenderer which is for 2D sprites, not UI images.
2fill in blank
medium

Complete the code to assign a texture to a RawImage component.

Unity
RawImage rawImg = GetComponent<RawImage>();
rawImg.[1] = myTexture;
Drag options to blanks, or click blank then click option'
Atexture
Bsprite
Cimage
Dmaterial
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sprite' property which belongs to Image component.
Trying to assign to 'material' instead of 'texture'.
3fill in blank
hard

Fix the error in the code to change the sprite of an Image component.

Unity
Image img = GetComponent<Image>();
img.[1] = newSprite;
Drag options to blanks, or click blank then click option'
Amaterial
Btexture
Cimage
Dsprite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'texture' instead of 'sprite' for Image component.
Trying to assign to 'material' which is unrelated here.
4fill in blank
hard

Fill both blanks to create a dictionary mapping GameObject names to their Image components if they have one.

Unity
var images = new Dictionary<string, Image>() {
    {gameObject.name, gameObject.GetComponent<[1]>()}
};
if(images[gameObject.name] != null && images[gameObject.name].[2] != null) {
    Debug.Log("Image found");
}
Drag options to blanks, or click blank then click option'
AImage
BRawImage
Csprite
Dtexture
Attempts:
3 left
💡 Hint
Common Mistakes
Using RawImage instead of Image in GetComponent.
Checking 'texture' property on Image component which does not exist.
5fill in blank
hard

Fill all three blanks to create a dictionary of GameObject names to their RawImage textures if the texture exists.

Unity
var rawImages = new Dictionary<string, Texture>() {
    {gameObject.name, gameObject.GetComponent<[1]>().[2]
};
if(rawImages[gameObject.name] != null && rawImages[gameObject.name].[3] != null) {
    Debug.Log("Texture found");
}
Drag options to blanks, or click blank then click option'
ARawImage
Btexture
Cwidth
Dsprite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sprite' property which belongs to Image, not RawImage.
Checking 'sprite' on texture which is invalid.