[FIXED] Canvas UIText's Transform.localPosition returning incorrect values? (Unity 5.3.2f1)

I don’t know if the title accurately describes what’s going on, but it sure looks that way to me.

I’m trying to make a news ticker for my game; the way it’s supposed to work is there are two identical UITexts that loop endlessly by shifting position to the right of the other UIText as soon as the first UIText rolls off the screen to the left. For whatever reason, the localPosition.x of the UIText’s Transform is reading differently than it does in the Editor, causing the text to loop prematurely. I put a Debug.Log() to print the localPosition.x, position.x, and preferredWidth and edited the script to pause the while() statement rather than move the text, so I can see exactly where everything lines up - sure enough the if() statement seems to be reading localPosition.x differently than Debug.Log does, despite running in the same frame on the same reference.


The editor the moment the coroutine pauses.

private int scrollSpeed = -2; //Set the scroll speed; negative values scroll to the left, positive to the right
    IEnumerator TickerScroll(){
        while(1==1){ //no idea how to keep a coroutine running without a while statement or constantly re-calling the coroutine... figured a simple while statement would be easier on the CPU
            Ticker[0].localPosition = Ticker[0].localPosition + Vector3.right * scrollSpeed; //Scroll the first UIText's transform
            Ticker[1].localPosition = Ticker[1].localPosition + Vector3.right * scrollSpeed; //scroll the second UIText's transform
            foreach(Transform ticker in Ticker){ //Loop through UITexts' transform
                if(ticker.localPosition.x <= -TickerText.preferredWidth){ //If the transform of the first UIText is (UIText.text.preferredWidth) pixels to the left of the screen
                    Debug.Log(ticker.localPosition.x + " " + ticker.position.x + " " + TickerText.preferredWidth); //Debug.Log to see exactly what's wrong with the if() statment... doesn't show anything out of the ordinary
                    ticker.localPosition = new Vector3(-ticker.localPosition.x, ticker.localPosition.y, ticker.localPosition.z); //Invert the localPosition.x of the transform; since the localPosition.x begins at 0 this should put it exactly to the right of the other UIText's transform
                }
            }

            yield return new WaitForFixedUpdate();
        }
    }

I have tried to wrap my head around what’s going on for the past 2 hours, and the only conclusions I can think of are either I did something wrong or I found a bug in the Unity editor. Anyone know what the deal is?

I am not sure if this helps but couldn’t you just create a UI text prefab and instantiate it, set the text to whatever it needs to be. use Transform.translate to move across the canvas, then destroy it once it is off the screen?

That’s actually what the first version of the script did, but it wouldn’t help to revert back to that because the issue lies in the timing; it’d still be destroying/instantiating them too early. The if() is running when the text still has about 2/5 of the screen left to cross.

FIXED IT!

Oddly enough it was the anchor causing the problem; I had the anchor of the RectTransform set to the center left so it would line up with the edge of the screen when the X coordinate was set to 0, but for some reason the engine was reading it as being dead center, causing it to trigger the if() statement exactly 400 pixels early each time. I have since modified the script to account for the extra 400 pixels and now it works just fine.