Search Unity

Instantiate on SpawnSpot don't work

Discussion in 'Scripting' started by Josenifftodd, Feb 9, 2016.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I'm trying to get my cards to stack up in one spot, I can't seem to get them to all line up on top of the spawn spot.

    Code (csharp):
    1.  
    2. public RectTransform deckSpawn;
    3.  
    4. for (int i = 0; i < shuffled_deck.Count; i++)
    5.             {
    6.                
    7.                 GameObject playableDeck = (GameObject)Instantiate(shuffled_deck[i]);
    8.                 playableDeck.transform.SetParent(deckSpawn.transform);
    9.  
    10.             }
    11.  
     
  2. Gilmar

    Gilmar

    Joined:
    Dec 17, 2013
    Posts:
    30
    you are just setting the transform parent, you aren't setting their position

    try something like this

    Code (csharp):
    1.  
    2. GameObject playableDeck = (GameObject)Instantiate(shuffled_deck[i]);
    3. playableDeck.transform.SetParent(deckSpawn.transform);
    4. playableDeck.transform.localPosition = new Vector3 (0f, card thickness, 0f);
    5.  
     
  3. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I tried that it didn't work, unity doesn't recognize 'card thickness' haha... thats the first thing i tried before just without card and put 0f instead, didn't work still :(
     
  4. Gilmar

    Gilmar

    Joined:
    Dec 17, 2013
    Posts:
    30
    card thickness should be replaced by the thickness of your cards eg 0.1f
     
  5. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    So I messed about a little and done this...
    Code (CSharp):
    1.  public RectTransform prefabdeck;
    2.     public GameObject spawnSpot;
    3.     public Transform c;
    4.  
    5.     // Update is called once per frame
    6.     public void OnClicked () {
    7.         Transform deck = Instantiate(prefabdeck[i], spawnSpot.transform.position, Quaternion.identity) as Transform;
    8.         deck.transform.SetParent(c, true); // -- C is Canvas which the card becomes child of
    Is they a way I can get it so i can spawn each card .1 above each other as they spawn in the same spot now :)