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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why transform position going to wrong place?

Discussion in 'Scripting' started by leegod, Jul 29, 2020.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,345
    Code (CSharp):
    1. public void MoveSelection(GameObject what, bool nowEquipUI)
    2.     {
    3.         Debug.Log("Move selection " + what.name + " " + what.transform.localPosition + " " + what.transform.position);
    4.         if (nowEquipUI)
    5.         {
    6.             SelectMark.transform.position = new Vector3(what.transform.position.x - MarkXEquipUI,
    7.                 what.transform.position.y, SelectMark.transform.position.z);
    8.             SelectBack.transform.position = new Vector3(what.transform.position.x,
    9.                    what.transform.position.y, SelectBack.transform.position.z);
    10.         }
    11.     }
    Code (CSharp):
    1. public void OnSelect(BaseEventData eventData)
    2.     {
    3.         SealManageUI.Instance.MoveSelection(gameObject, true);
    4.     }
    Why this code not work at first time when each slot gameobject instantiated,

    but works well when move selection by keyboard arrow up/down key after wrong placement?

    movement1.PNG movement2.PNG
     
  2. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Possibly because your prefab has some kind of default offset saved to it, causing it to start in the wrong place. But it's hard to tell without seeing the instantiation code (assuming the selector thingy is instantiated by the script).
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,345
    solved by using coroutine and waitendofframe inside of it.

    So prefab's initial position after instantiation is different with user seeing firstly(most above from list).