Calling coroutine in a method issue.
Hi, I want to move a button via coroutine from a method however this problem occurred.
void Start(){
menu_btn = Instantiate (menuPrefab, new Vector3 (-50f, 0f, 0f), Quaternion.identity) as GameObject;
menu_btn.GetComponent<RectTransform> ().SetParent (canvas.transform, false);
pauseObjects.Add (menu_btn);
StartCoroutine (SmoothMovement(menu_btn, menu_btn.GetComponent<RectTransform> ().localPosition + new Vector3 (0f, 100f, 0f))); // This line works but I don't want to call it on start.
}
This is normally work but when I removed the StartCoroutine line above and add the following method.
void showButton(){
StartCoroutine (SmoothMovement(menu_btn, menu_btn.GetComponent<RectTransform> ().localPosition + new Vector3 (0f, 100f, 0f)));
}
And when I call this method my coroutine just move up 0.XXX point and it seem to never finish the coroutine (Actually, it just move one bit toward the point I marked.). And this is my coroutine code
IEnumerator SmoothMovement(GameObject button,Vector3 end){
float sqrRemainingDistance = (button.GetComponent<RectTransform>().localPosition - end).sqrMagnitude;
while (sqrRemainingDistance > float.Epsilon) {
Vector3 newPosition = Vector3.MoveTowards(button.GetComponent<RectTransform>().localPosition,end,inversedMoveTime*Time.deltaTime);
button.GetComponent<RectTransform>().localPosition = newPosition;
sqrRemainingDistance = (button.GetComponent<RectTransform>().localPosition - end).sqrMagnitude;
yield return null;
}
}
Please help me. Thank you.
Comment