How to Access a Text Value in a ScrollView?

Hello! My brain is turning to mush!

So it should be pretty simple: I have a Prefab with several text objects on it, and they all work. That is, except for the most important one, which is inside of a ScrollView!


Here’s the layout of the prefab:

MyAwesomePrefabGameObject

- txtTitleText
- txtAuthorText
- Scroll View / Viewport / Content / txtStoryText

The code for the first two Game Objects works just fine!

 myTitleText = MyPrefabTransform.Find("txtTitleText").GetComponent<Text>();
 myAuthorText = MyPrefabTransform.Find("txtAuthorText").GetComponent<Text>();

The problem is that I can’t figure out how to access the text inside the Scroll View!

myStoryText = MyPrefabTransform.Find("txtStoryText").GetComponent<Text>();

This didn’t work. So I read the manual, which game me some ideas to try.

myStoryText = MyPrefabTransform.FindChild("txtStoryText").GetComponent<Text>();
myStoryText = MyPrefabTransform.Find("txtStoryText").GetComponentInChild<Text>();
myStoryText = MyPrefabTransform.Find("ScrollView/Viewport/Content/txtStoryText").GetComponent<Text>();
myStoryText = MyPrefabTransform.Find("ScrollView/contentContainer/txtStoryText").GetComponent<Text>();

And none of those worked. I kept going, trying to change little things here and there, but nothing worked. I have spent three days on this, and have found nothing so far. The ScrollView.contentContainer looked hopeful, but no luck yet.

Thanks ahead of time for taking a moment! Hope you’re having a safe and satisfying weekend!

Well, I found an answer. The answer is: Scroll View is garbage!

All kidding aside, let’s talk.

Instead of putting your Scrollable Text inside of an unweildy Scroll View, just create a new panel, and put your Text Object there. Add the following items to your new panel:

  • A Scroll Rect - Drop your text game
    object into the Content Slot. Uncheck
    Horizontal. Drag the panel into the
    Viewport slot. When I first added this component, it kinda turned the panel inside out, making it look like a red X. I started dragging some values around and I realized I could actually resize it.
  • A Content Size Filter - Leave the Horizontal Fit at Unconstrained, but set the Vertical Fit to Preferred Size.
  • A Grid Layout Group - Set your Cell Size to the required size. In my case, it was 700x400. I went back and forth between this and the dimension of the panel for a few before I realized what was up.
  • A Mask - Finally, you need a mask to block out anything outside of your viewport area.

When I attempted to access my text object last time, I used the following line:

myStoryText = MyPrefabTransform.Find("txtStoryText").GetComponent<Text>();

I tried all sorts of variations, but the long and the short of it was that trying to access anything within the classic Scroll View element was futile beyond belief! I can’t believe how much time I spent on this! I watched the entire UI series on Unity Premium(it’s free till the 20th of June 2020 because of the thing), all of the new scripting tutorials they put out on instantiation and at least three dozen more independent tutorials on the subject before I decided to ditch the Scroll View and simply add the needed elements to a panel.

See, the very first thing I did was to take the Text Object out of the scroll View to see if the data would connect again properly. It did. Then I created a new Panel, and added the Text Object to the Panel. I went into my script and changed the line to:

myStoryText = MyPrefabTransform.Find("Panel/txtStoryText").GetComponent<Text>();

And it worked. When I realized I was back on track, I simply added all the stuff I needed, like a Scroll Rect component, directly to the new Panel.

TLDR: Scroll View is not good for making text scroll. Use a Panel instead.