GetComponentsInChildren Problem

Dear: Reader

What I am trying to do is have all my objects under “playModeUI” that have the script “dragAndDropUI” to run there function “TurnOnDragUI()”.

Script “A”

using UnityEngine;
using System.Collections;

public class handlerUI: MonoBehaviour 
{
	public GameObject[] playModeUI;

	public void TurnOnDragUI() 
	{
		playModeUI = GameObject.GetComponentsInChildren<dragAndDropUI>();
		
		foreach (dragAndDropUI script in playModeUI) 
		{
			script.TurnOnDragUI();
		}
	}
}

James

So I’m gonna say that that doesn’t work because the array elements are GameObjects not dragAndDropUI objects. This is because the array was made as GameObject.
So, instead make your array like this:

public dragAndDropUI[] playModeUI;

If that doesn’t work you can also use the Monodevelop type, but that will probably give you the same problem. If the GetComponentsinChildren isn’t working you can put Monodevelop into that as well, but that will return all scripts.

Edit: Final Code from comments, with additional notes from me

 using UnityEngine;
 using System.Collections;
 
 public class togglePanelUI : MonoBehaviour 
 {
 //public GameObject for referenceing the UI Canvas, drag canvas into variable in inspector
 public GameObject canvasUI;

     public void DragUI()
     {
 // make a new array of the script's class name, 
 // find all components in canvas with the same script/class name
         dragAndDropUI[] tos = canvasUI.GetComponentsInChildren<dragAndDropUI> ();
 // iterate through all of the selected scripts and call the same method on them
         foreach(dragAndDropUI scripts in tos)
         {
             scripts.TurnOnDragUI();
         }
     }
 }