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?