- Home /
Random Children Array that changes Parent, not working right
I'm making a CCG and wrote a script that handles pulling random cards from the deck and it's working, kind of... You see, whats going on is it's pulling the children of the prefab instead of just the prefab itself (see image). What am I dong wrong here?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardDraw : MonoBehaviour {
public GameObject deckZone;
public GameObject handZone;
public bool gameStart = true;
public bool turnStart = false;
public int startingHand = 0;
void Update () {
if (gameStart == true) {
gameStart = false;
Debug.Log ("Start Game Draw");
int totalNumberOfCardsInDeck = deckZone.transform.childCount;
Debug.Log (totalNumberOfCardsInDeck);
while (startingHand > 0) {
Transform[] deckZoneChildren = deckZone.GetComponentsInChildren<Transform> ();
GameObject randomCard = (GameObject)((Transform)deckZoneChildren [Random.Range (0, deckZoneChildren.Length)]).gameObject;
randomCard.transform.parent = handZone.transform;
Debug.Log ("player drew " + randomCard);
startingHand--;
}
} else {
if (turnStart == true) {
Transform[] deckZoneChildren = deckZone.GetComponentsInChildren<Transform> ();
GameObject randomCard = (GameObject)((Transform)deckZoneChildren [Random.Range (0, deckZoneChildren.Length)]).gameObject;
randomCard.transform.parent = handZone.transform;
Debug.Log ("Player drew " + randomCard);
turnStart = false;
}
}
}
}
Good day.
I don't get what you want and what is happening (Please note most of us are not English, so help us understanding what you need :D )
I see you access components of children, and then look for the parent. But don't see what problem you have and what you want to get.
Bye!
the card prefab is made of a bunch if images and text, the problem is the script is pulling those different parts of the prefab and moving it to the hand location. I want it to only move the entire perfab.
I'm not really sure, but I think you can't move prefabs, as they aren't really loaded on the scene.
A way around this would be creating an Instance of the prefab, disabling it, and using that instance as your new prefab (The instance would be in your scene, so you can move it)
That said, a prefab's parent isn't the prefab itself. A parent is whatever object contains your object in the scene's hierarchy.
Also, to move a Transform, you need to set it's position, not the transform itself
//card.transform.parent = hand.transform
card.transform.parent.position = hand.transform.position
Your answer
Follow this Question
Related Questions
Pick between two floats 2 Answers
Distribute terrain in zones 3 Answers
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
Random array spawn C# 1 Answer