0

I just wanted to set the value of the scrollbar of my scrollview object to 0 every time a new text object is created. This way the newest object would always remain at the bottom without needing to scroll down (think of the chat of a video game for example). My problem is that for some reason the program is setting the value to 0 and then creating the text object, so it keeps the 2nd newest object at the bottom of the scrollview.

My code:

//Creates the Text objects
private GameObject GenerateTextObject(string content)
{
    //Creates the Text objects
    GameObject messagePrefab = Instantiate(MessagePrefab);
    Text textComponent = messagePrefab.GetComponent<Text>();

    //Sets the content of the text and parent
    textComponent.text = content;
    messagePrefab.transform.SetParent(ContentPanel.transform, false);

    ScrollbarVertical.value = 0;

    return messagePrefab;
}

In the code I am setting the value at the end of the function, but still sets the value of the scrollbar to 0 (properly moving the scrollview to the bottom) before the object is created.

https://gyazo.com/897982521f13d7792ec26540490a40c0 In the Gyazo picture you can see how it doesn't scroll all the way down.

I have tried using a coroutine and waitForEndFrame aswell as waitforseconds(1), but none seem to work.

Edit: when loading up Unity and sending new messages to the scrollview, I see the scrollbar go all the way down and then really quickly move up just a bit hiding the new text object.

  • Change `elasticity` of the ScrollRect to `0` then do `ScrollbarVertical.value = 0;`. This is just an idea. Not sure if that will even work. – Programmer Jul 24 '17 at 05:06
  • No... I have tried tweaking with all the ScrollRect options, and nothing seems to do the trick. :(, Thanks a lot for helping once again Programmer – Paco Martos Triguero Jul 24 '17 at 06:05
  • After a lot of thinking and messing around with different "fixes" I have come up with a workaround for this problem. Once again, thanks for trying to help :) – Paco Martos Triguero Jul 24 '17 at 06:16
  • 1
    I was about to tell you to try [this](https://stackoverflow.com/a/30769550/3785314). Go ahead and put your answer as that will help other people. You are welcome! – Programmer Jul 24 '17 at 06:17

3 Answers3

6

What you can do is create a new Scroll View and follow these steps:

enter image description here

The result is from calling GenerateTextObject(Time.time.ToString()); every second.

private GameObject GenerateTextObject(string content)
{
    //Creates the Text objects
    GameObject messagePrefab = Instantiate(MessagePrefab);
    Text textComponent = messagePrefab.GetComponent<Text>();

    //Sets the content of the text and parent
    textComponent.text = content;
    messagePrefab.transform.SetParent(ContentPanel.transform, false); 

    //Force reset to the bottom on each message
    //ScrollbarVertical.value = 0;

    return messagePrefab;
}
Pluto
  • 3,911
  • 13
  • 21
1

I have not come up with a proper fix for this problem (I honestly do not know the cause of problem), but I have put together a little work-around for this situation.

To do this I simply created a small function:

public void Testing()
{
    if (testing == true)
    {
        ScrollbarVertical.value = 0;
        testing = false;
    }

}

This would be attached to the built-in functionality of scrollbars "On Value Changed (Single)". In order to allow this to work, I just change the bool "testing" to true after creating a text object on GenerateTextObject(string content).

This fix is really ugly, and probably would cause more problems in the near future, but for now, it is a quick and dirty solution for this problem.

  • Where do you call that function? – Programmer Jul 24 '17 at 06:29
  • I am not sure how it is properly called (I am really new in unity and programming in general) but on the inspector, under the Scrollbar Vertical object, there is a small place called "On Value Changed", this is where I added it. https://gyazo.com/5483eabac4b30171d6306facc60236ba – Paco Martos Triguero Jul 24 '17 at 06:31
  • Oops. I missed that you mentioned *OnValueChanged*. Got it. – Programmer Jul 24 '17 at 06:35
1

Sorry to necro this, but I had a similar problem and googling couldn't find a good answer. So I'm posting the solution that I found.

I tried to change the scrollbar value to 1 or 0, depending on the direction, so that everytime I open a UI panel, it'll start at the top. But no matter what I did, nothing seemed to work.

So my solution is to change the Y position of the content instead of the scrollbar value.

My scrollbar direction is set at bottom to top. Then I take the sizeDelta of the content, divide it by 2, (since I need the value of 1 for the scrollbar.value). I then multiplied it by -1 (if I dont, it'll start at the bottom). Then I set that value as the Y position of the content.

So far it works like a charm everytime I open my UI panel.

Hope this helps anyone looking for a solution.

ouflak
  • 2,458
  • 10
  • 44
  • 49