Scrollbar : does not refresh when we remove items

Hey.

So I’m doing a chat system and the user has the possibility of filtering messages. Basically, I just add the messages within a container and, when the user checks or unchecks a filter, it hides the messages corresponding to the “section”.

Every time I add a message, I set the value of my scrollbar to zero in order to see the last message received. The only thing it’s when I hide some message, the content is not “refresh” (there just is a blank).

I made a GIF just for you

1924380--124276--chatFail.gif

So, as you can see, I add several messages and then I click on “General” in order to hide General messages. The scrollbar does something weird (his size) and the content shows nothing (but there are some messages but we can’t see them because of the mask). But when I move a little bit (with the mouse) the scrollbar, it will be back to the normal…

So I was wondering if you guys have an idea on how to solve this ?

Code:

void Update() {
            if (Input.GetKeyDown(KeyCode.Space)) {
                Message.eSection randomSection = (Message.eSection)UnityEngine.Random.Range(0, 3);
                addMessage(randomSection, "My message comes from " + randomSection, "sbizz");
            }
        }

        public void addMessage(Message.eSection section, string message, string sender = "") {
            ChatElements.Filter filter = getFilter(section);

            if (filter == null) {
                Debug.LogWarning(section + " was not found in the filters list");
            }

            Text go = Instantiate(_gui.message) as Text;
            Message msg = go.gameObject.AddComponent<Message>();

            msg.section = section;
            msg.dateSend = DateTime.Now;
            msg.message = message;
            msg.sender = sender;

            go.gameObject.SetActive(filter != null && filter._isChecked);
            go.transform.SetParent(_gui.containerMessages, false);
            go.text = "";

            if (printDateNow) {
                go.text += "[" + msg.dateSend.ToString("H:mm:ss") + "] ";
            }

            if (section == Message.eSection.MESSAGE) {
                if (sender == "") {
                    Debug.LogWarning("No sender specified");
                }

                go.text += sender + ": ";
            }

            go.text += message;
            _messages.Add(msg);
            _gui.scrollbar.value = 0;
        }

        public ChatElements.Filter getFilter(Message.eSection section) {
            foreach (ChatElements.Filter filter in _filters) {
                if (filter.showType.Contains(section)) {
                    return filter;
                }
            }

            return null;
        }

        public void applyFilters(ChatElements.Filter filter) {
            foreach (Message message in _messages) {
                if (filter.showType.Contains(message.section)) {
                    message.gameObject.SetActive(filter._isChecked);
                }
            }

            _gui.scrollbar.value = 0;
        }

So I found a solution BUT I don’t like it. I mean, I’d like to avoid this terrible solution.

  • First, I changed the Content pivot and set it to middle top.
  • Then (this is the thing I want to avoid), I changed something in my code. I supposed that Unity has to recalculate some graphic stuff (the scrollbar is linked to the scrollrect which is linked to the Content), so I just wait some frames until I change the Scrollbar value…
        private IEnumerator refreshScrollBar() {
            yield return null;
            yield return null;

            _gui.scrollbar.value = 0;
        }

:hushed:

If someone has a better solution, feel free to tell it to me :smile:

I was doing some tests with adding and removing items from a scrolling list and found that after any add/remove operation I had to recalculate the content size to have the scrollbar work correctly.

I call SetSizeWithCurrentAnchors on the content RectTransform.

1 Like

Hmm, I’m gonna try this evening and tell you if it works !

Thanks ! :slight_smile:

Okay, it works so that super cool! Doing a coroutine just for that, it was like killing fishes with a bazooka.

Now I need to find why my Content leaves a blank when I remove some items. I think I found the problem but didn’t have the time to check ; maybe the anchor is no longer valid and I have to reset it to zero. I’m gonna try this evening.

Okaayy ! So I did something smart (very rare).

I just changed the pivot… to bottom middle. I don’t need code anymore. I’m soooo stupid :p:smile:

Thanks anyway :wink:

Hi, i have same problem, but i don’t know how can i do it. Can u share your solution with me? thanks

I already got it :smile: