Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Bizarre problem with animating a UI Panel in a Coroutine

Discussion in 'Scripting' started by SkrenZz, Jun 26, 2020.

  1. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    26
    [EDIT: The issue was solved]

    Hello there,

    I am posting this message because of a very strange bug (I assume?) I am facing in Unity. I am trying to animate a UI Panel to slide in and out using a coroutine. Unfortunately, I can not animate it regularly with Animator, because the position it should slide to is dynamic and depends on the width of the visible characters of the text inside it.

    I am changing the x value of the RectTransform.anchoredPosition. Using the "Watch" window in Visual Studio (debug mode), I can see that the the value is indeed being changed, but it is being reset to its original position by the time the loop happens again. Here is the code:
    Code (CSharp):
    1. // slide in
    2.         while (this.rectTransform.anchoredPosition.x > destinationX)
    3.         {
    4.             yield return new WaitForSeconds(this.delayPerStep); // delay per step = 0.2
    5.             this.rectTransform.anchoredPosition -= new Vector2(this.stepSize, 0f); // step size = 10
    6.             Debug.Log("Panel position: " + this.rectTransform.anchoredPosition.x);
    7.         }
    8.         this.rectTransform.anchoredPosition = new Vector2(destinationX, positionY);
    And here is a screenshot of the console:
    upload_2020-6-27_0-5-12.png

    I am so confused, I really can't tell what is wrong here. There is only one line of code that sets the position's X to 1382 which happens at the Start method (except for the lines of code responsible for sliding out which happens after the slide in code in the same coroutine. I also ensured it is not somehow happening simultaneously using a breakpoint, and it is indeed not the issue).

    I will thank anyone who may try to help me, I am clueless and any attempt to help will be very appreciated.

    Have a great day and many thanks in advance!
     
    Last edited: Jun 26, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Looks like it's working fine to me? Your step size is 10, you're starting at 1382 and ending at 1372 so you get there in one step. What's destinationX supposed to be?
     
    SkrenZz likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Are you sure you removed any previous animations that can happen to that object? Animation will override pretty much anything else you do in script. One way to test with this is to rename the panel (just change its name slightly), since Unity animations are hooked up by the GameObject name.
     
    SkrenZz and PraetorBlue like this.
  4. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    26
    Somewhere around -200 in the specific case.

    You were absolutely right, I forgot I had an Animator component that continuously changed the position, after removing it- the animation is working as intended!

    Thank you both very much!!!
     
    Kurt-Dekker and PraetorBlue like this.