Search Unity

Cant set my prefab position with Instanciate(Prefab)

Discussion in 'Scripting' started by Korigoth, Oct 9, 2015.

  1. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    I'm trying to create a list of Prefab for my uGUI but when i try to position the prefab it doesnt move to the position at all... if i check in the scene it's at an other place completly... -561, 167, 0 ... how do i move my prefab??

    he's a child of the current monobehaviour script.

    Code (CSharp):
    1.  
    2. for (var i = 0; i < SaveGames.Count; i++)
    3. {
    4.                 var savedGame = Instantiate(SavedGamePanelPrefab);
    5.                 savedGame.SetInfos(SaveGames[i]);
    6.                 savedGame.transform.parent = _loadMenuPanel.transform;
    7.                 var rectTransform = (RectTransform) savedGame.transform;
    8.                 rectTransform.localScale = Vector3.one;
    9.                 rectTransform.sizeDelta = Vector2.zero;
    10.                 rectTransform.anchoredPosition = Vector2.zero;
    11.                 rectTransform.position = new Vector3(14, 131 * 75, 0);
    12.                 savedGame.gameObject.SetActive(true);
    13. }
     
  2. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    var rectTransform = savedGame.GetComponent<RectTransform>();

    The recttransform is not inherently built into "transform", you'll have to manually access it as a component.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Nonsense. RectTransform inherits from Transform. So you can access the RectTransform with .transform

    To the OP: Setting positions manually for UI elements gets a bit weird. Try setting the position first, before parenting. Then parent with Transform.SetParent and set the second argument to false.
     
  4. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Apologies o_O, I have some vague memory of not being able to do that when I first tried the 4.6 gui. Guess I'll be rewriting some of my code.

    Also, you might try localPosition with local vectors *after* parenting as well. Both should work.
     
    Kiwasi likes this.
  5. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    still doesnt work with

    Code (CSharp):
    1. for (var i = 0; i < SaveGames.Count; i++)
    2. {
    3.     var savedGame = Instantiate(SavedGamePanelPrefab);
    4.     savedGame.SetInfos(SaveGames[i]);
    5.  
    6.     var rectTransform = (RectTransform)savedGame.transform;
    7.     rectTransform.localScale = Vector3.one;
    8.     rectTransform.sizeDelta = Vector2.zero;
    9.     rectTransform.anchoredPosition = Vector2.zero;
    10.     rectTransform.position = new Vector3(14, 131 + (75 * i), 0);
    11.     savedGame.transform.SetParent(_loadMenuPanel.transform);
    12.     savedGame.gameObject.SetActive(true);
    13. }
    the position i get is : -563, 9527, 0

    prefab default : 0, 0, 0
    parent : 227.5, 110.5, 0

    EDIT

    if i add

    rectTransform.localPosition = Vector3.one;

    the position i get is : -562, -167
     
    Last edited: Oct 9, 2015
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    See if using .SetParent(_loadMenuPanel.transform, false) helps. The second parameter will make it keep its local position (which you set in the line above it) - the default is to keep the world position instead.
     
    Kiwasi likes this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Also, in this case, you may sidestep the issue entirely by using a Vertical Layout group on the parent, instead of setting each position by hand. This is what it was made for.
     
    Kiwasi and Mycroft like this.
  8. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    i forgot about the false parameter it was written up...

    thx a lot to tell it back! it work as expected now.

    I will for sure take a look at Vertical Layout for my parent. its surely the UI thing i was looking for!
     
    StarManta likes this.