Adding Pictures to the Text Adventure

Hello! So I’m trying to make something like a picture book and I tried to SerializeField sprites like I did text? But I failed miserably. My text is shown correctly from the SerializeField StoryText area, but the Sprite is not even seen (even if I try to do it from the Hierarchy)

Please help? I’m so so rusty, but I really want to finish this around this time for Christmas.


Hi Asli,

You are on the right track, so you definitely did not fail. All you have to do is to change a few things. The Canvas may contain UI elements only. SpriteRenderer is not a UI element. For this reason, replace it with an Image component.

In your code, you want to control the Image component, not a Sprite. Unfortunately, the Unity API is not reliable at the moment, for this reason, if Image does not work, add the UnityEngine.UIElements namespace at the top of your code.

Check with the dot notation what properties you can access. If you named your Image variable display, look for display.sprite and display.image. The compiler in Visual Studio will complain if the property is not available.

Depending on what the compiler accepts, hover your mouse over it to see the type of the property. Use that type in your State class.

As you can see, this is mainly about changing names. The rest of your code is perfectly fine.


See also:

THANK YOU!! Aww, you are so encouraging, thanks a bunch! Have a wonderful day :slight_smile:

Oh sorry :frowning: couldn’t manage it. I named my image variable imageComponent and after the . the only viable option is Sprite. And VS doesn’t accept it because I can’t turn Sprite into an Image. Any other advice? One another way I thought of is… what if I did the same thing with Scenes instead of States? (I would rather solve the image issue here though.)

What does your State.cs script look like? There’s something wrong with your GetImage() method (indicated by the red squiggly underline). Did you update it to use an Image instead of a Sprite?

You have a point, I did, but because I tried to use ImageComponent.sprite = state.GetImage(); VS gave me an error. (Because a sprite is not an image) I turned it back to this now, but still doesn’t work because I have no idea what to write after . in imageComponent. = state.GetImage();

So I just implemented this myself in my copy of the Text 101 game and you’re super close, and I may have pointed you in the wrong direction with changing your State.cs file a little prematurely. Look at these items and I think you might get there with a little nudge:

  • If you need to access the text property on textComponent to update the text (AdventureGame.cs, Line 17), what property do you need to access on the imageComponent to change the image? (Hint: The answer is in this Unity docs page: Unity Scripting API: Image)
  • Once you have the right property for imageComponent, what do you need to do to GetImage() to return the right type?

And here’s a freebie, make sure that the Texture Type for the image you want to bring in is set to "Sprite (2D and UI).

Well done so far! As @Justin_Abbott wrote, you are super close. :slight_smile:

OMG I THINK I DID IT!!! Thanks!!! :smiley:

1 Like

Awesome! :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms