Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Random Children Array that changes Parent

Discussion in 'Scripting' started by midgear, May 13, 2018.

  1. midgear

    midgear

    Joined:
    Apr 5, 2013
    Posts:
    9
    I'm making a CCG and wrote a script that handles pulling random cards from the deck. The script is working, kind of... You see, whats going on is it has an array that gets the transform of the children and grandchildren of whatever is inside the public GameObject deckZone; and moving random GameObjects from the Array to the public GameObject handZone; (see image) but that's not what I want it to do.

    What I want it do do is just grab the children (not grandchildren) of the deckZone add it to the array and move a random GameObjects from said array to the handZone.

    here is my script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CardDraw : MonoBehaviour {
    5.      public GameObject deckZone;
    6.      public GameObject handZone;
    7.      public bool gameStart = true;
    8.      public bool turnStart = false;
    9.      public int startingHand = 0;
    10.      void Update () {
    11.          if (gameStart == true) {
    12.              gameStart = false;
    13.              Debug.Log ("Start Game Draw");
    14.              int totalNumberOfCardsInDeck = deckZone.transform.childCount;
    15.              Debug.Log (totalNumberOfCardsInDeck);
    16.              while (startingHand > 0) {
    17.                  Transform[] deckZoneChildren = deckZone.GetComponentsInChildren<Transform> ();
    18.                  GameObject randomCard = (GameObject)((Transform)deckZoneChildren [Random.Range (0, deckZoneChildren.Length)]).gameObject;
    19.                  randomCard.transform.parent = handZone.transform;
    20.                  Debug.Log ("player drew " + randomCard);
    21.                  startingHand--;
    22.              }
    23.          } else {
    24.        
    25.              if (turnStart == true) {
    26.            
    27.                  Transform[] deckZoneChildren = deckZone.GetComponentsInChildren<Transform> ();
    28.                  GameObject randomCard = (GameObject)((Transform)deckZoneChildren [Random.Range (0, deckZoneChildren.Length)]).gameObject;
    29.                  randomCard.transform.parent = handZone.transform;
    30.                  Debug.Log ("Player drew " + randomCard);
    31.                  turnStart = false;
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    643
    Instead of GetComponentsInChildren you have to use GetChild:
    Code (CSharp):
    1. Transform randomCard = deckZone.transform.GetChild(Random.Range(0, deckZone.transform.childCount));
     
    Last edited: May 14, 2018
    midgear likes this.
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,406
    deckZone.childCunt willl not give overflow

    edit. In this case it could be good having a "Viewmodel" and not use the gameobjects as the data model
     
  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    643
    I edited to use deckZone.transform.childCount, but what do you mean with overflow? It should be fine when cards are removed from the deck.
    Otherwise yes using lists of card objects rather than game objects is bette.

    LOL
     
  5. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,406
    GetChild only operates on the direct children of deckZone while deckZone.GetComponentsInChildren<Transform> will return also children of your children. So you will get an overflow since deckZoneChildren is based on the length of GetComponentsInChildren
     
  6. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    643
    He only wants direct children so I suggested to use GetChild and childCount. GetComponentsInChildren is not needed. I edited the code after your comment as I missed to replace deckZoneChildren.Length with deckZone.transform.childCount.
     
    AndersMalmgren likes this.
  7. midgear

    midgear

    Joined:
    Apr 5, 2013
    Posts:
    9
    Yatta! That did it! thank you sooooo much!

    PS I'm a she btw.
     
unityunity