- Home /
Only one Child Component turning off.
Im making a card game and I have a script that makes the cards draggable , but I want it so when they are in the discard pile draggadle is turned off. I wrote a code to do this but for some reason it's only turning off the Component on the first card in the discard pile.
Card 1:
Card 2+:
what am I doing wrong?
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InDiscard : MonoBehaviour {
public bool inDiscardPile = true;
public void Start(){
InvokeRepeating ("inDiscardCheck", 2.0f, 0.3f);
}
void inDiscardCheck(){
if (inDiscardPile == true) {
//Debug.Log ("true");
GetComponentInChildren<Draggable>().enabled = false; }
else {
//Debug.Log ("false");
GetComponentInChildren<Draggable>().enabled = true;
}
}
}
Answer by iJuan · May 11, 2018 at 11:30 PM
Hi, what you're looking for is GetComponentsInChildren(), which returns an array of all components found, then you would iterate over them using a foreach, and modifying them all
foreach (Draggable drag in GetComponentsInChildren()) {
//Your code
}
Answer by tormentoarmagedoom · May 11, 2018 at 11:20 PM
Good day.
You are using GetComponentInChildren, which looks for a component of a child object. So looks for the script. If found one stops looking.
You maybe want to use CardObject.GetComponent in every card you need. So assing every card to a gameobject variable and do that for each card ypu want to disable its script.
Bye
Your answer
![](https://koobas.hobune.stream/wayback/20220612162506im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Finding a panel in canvas 2 Answers
How to DontDestroyOnLoad a specific ui of a canvas? 1 Answer
Get .prefab (object from unity) with component from assets. 1 Answer