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;
		}
	}
}

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
  }

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