- Home /
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
Answer by daneislazy · Feb 08, 2015 at 06:02 PM
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();
}
}
}
public GameObject[] play$$anonymous$$odeUI;
is where i am going to drop my UGUI canvas
I am trying to access all the scripts.
Trying to call the function on all the gameobjects with the "dragAndDropUI" scipt.
oh, ok. I didn't realize you were using it for something else.
You should just be able to make a new array using the same idea. I had to do some thing similar recently; you need to store the scripts as whatever class they are or as $$anonymous$$onodevelop
. If you use $$anonymous$$onodevelop
for the array creation and the GetComponentsInChildren
call that should let you use the array elements just like a GetComponent
would. Probably check the name of the component to see if it is dragAndDropUI
or whatever it pirints/debug.logs as.
Im not sure if you can just do script.TurnOnDragUI()
at that point, you might need script.dragAndDropUI.TurnOnDragUI()
.
And make sure the TurnOnDragUI()
is public.
Edit: oh also if you use $$anonymous$$onodevelop
it will return all UI things like Button Image and Text because they are scripts.
Sorry, but how would I go about in writing that in script? and yes the function is public
oh, if play$$anonymous$$odeUI is just the one canvas it doesn't need to be an array, assu$$anonymous$$g that:
dragAndDropUI[] dragArray = play$$anonymous$$odeUI.GetComponentsInChildren<dragAndDropUI>();
foreach (dragAndDropUI script in dragArray)
{
script.TurnOnDragUI();
}
Try that.
Your answer
Follow this Question
Related Questions
Get SkinnedMeshRenderer as Renderer? 1 Answer
GetComponentsInChildren() doesn't works like I expect 1 Answer
Invalid Cast error when my format seems the same as the script reference example? 1 Answer
GetComponentsInChildren contains null elements when fetching MeshRenderers 1 Answer
Is it not possible to include "if-then" conditionals within foreach loops? 1 Answer