0

I'm having a trouble of getting the child from an array of GameObject

Here's my script:

[SerializeField]
GameObject[] CameraScriptsTV;

protected override void SetPosition()
{
CameraScriptsTV = this.gameObject.transform.GetChild(0).gameObject; //here is the error
   foreach (GameObject TV_CameraScript in CameraScriptsTV)
      {
         TV_CameraScript.GetComponent<Bloom>().enabled = false;
      }
}

I'm getting the child using what has been written on Transform.GetChild.html

What I am trying to do is like this for example:

In this script I get the child from the Camera in my hierarchy : Image Reference

CameraScriptsAir_1 = this.transform.GetChild(0).GetChild(1).GetChild(0).GetChild(2).GetChild(0).gameObject;
CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = false;

So what i am getting here is the AIR_1(Clone) then get the script Bloom.cs and DepthOfFieldDeprecated.cs.

I hope its clear.

7
  • What error are you getting? Commented Jan 15, 2018 at 5:51
  • GetChild(0) returns a single Transform. Getting that single Transfrom's GameObject can never, and will never, result in an array of anything. Commented Jan 15, 2018 at 5:59
  • @Draco18s actually that was just an example of how i'm getting a child sir. Commented Jan 15, 2018 at 6:08
  • @Steve could not convert GameObject to GameObject[] something like that Commented Jan 15, 2018 at 6:13
  • @NoobProgrammer then you're hitting the problem Draco18s is describing. Try creating a list and adding the gameobject to it. Commented Jan 15, 2018 at 6:16

3 Answers 3

1

I think what you wanted to do is this

protected override void SetPosition()
 { 
 foreach (Transform TV_CameraScript in this.transform)
 { 
         TV_CameraScript.GetComponent<Bloom>().enabled = false;
 } 
}

I don't understand why you would want an array of gameobjects just to modify a component.

Sign up to request clarification or add additional context in comments.

3 Comments

To which object is the current script attached ?
Then why the extra getChilds()?
I uses it instead of GameObject.Find cause its way more faster. And i put it on an array because they're both the same path.
1

Well, there's another shortcut for you to achieve this, you can get all the Bloom component with GetComponentsInChildren();

Here's the solution.

[SerializeField]
Bloom[] bloomComponents;

protected override void SetPosition()
{
    //Get all Bloom components from child, and store it in global variable
    bloomComponents = this.gameObject.GetComponentsInChildren<Bloom>();

    //Loop to it and disable it
    foreach (Bloom bloom in bloomComponents)
    {
        bloom.enabled = false;
    }
}

EDIT 1

If your transform has more than one branch, we need to find it recursively, which we will do with some helper method.!

Don't forget to include using System.Collections.Generic in top of your script, because we use List<> Class

[SerializeField]
Bloom[] bloomComponents;

protected override void SetPosition()
{
    //Only do it once, we don't want to make it expensive
    if (bloomComponents.Length == 0)
    {
        //Get all Bloom components from child, and store it in global variable
        bloomComponents = GetComponentsInChildrenRecursively<Bloom>(this.transform);
    }

    //Loop to it and disable it
    foreach (Bloom bloom in bloomComponents)
    {
        bloom.enabled = false;
    }
}

//Autoatically Find Component Recursively
public T[] GetComponentsInChildrenRecursively <T>(Transform root)
{
    List<T> results = new List<T>();
    if (root.childCount > 0)
    {
        foreach (Transform t in root)
        {
            //this root has transform in it, so recursive
            //it will call GetComponentsInChildrenRecursively over and over again, till no child
            results.AddRange(GetComponentsInChildrenRecursively<T>(t));
        }
    }

    //Add to the results
    results.AddRange(root.gameObject.GetComponentsInChildren<T>());
    return results.ToArray();
}

if this show some error, please tell me, as my office laptop don't have unity IDE, i can't test it.

Please Note : don't use this in very huge Transform Hierarchy, since it will loop through all of it.

10 Comments

How can it find the bloom script sir from all of those components ? Could you explain sir
gameObject.GetComponentsInChildren<Bloom>(); will find all script type of Bloom inside the gameObject, so you don't have to trace around the transform.GetChild(), it will automatically do the job, see reference docs.unity3d.com/ScriptReference/…
So i don't have to drag anything on the inspector . Cause what my code doing is I'm not dragging them anymore they're automatically been place on the components.
It do the same as your codes, the only difference is it way more simpler and readable. it will drag (or u should call it, 'Apply') all bloom components to the inspector.
it's up to you whether you want to use this way or another way, because in programming there is a lot of way in doing a same thing ;)
|
0

I Solved it by doing it like this

CameraScriptsTV[0] = this.transform.GetChild(0).GetChild(1).GetChild(5).GetChild(0).GetChild(0).gameObject;
CameraScriptsTV[1] = this.transform.GetChild(0).GetChild(1).GetChild(6).GetChild(0).GetChild(0).gameObject;
CameraScriptsTV[2] = this.transform.GetChild(0).GetChild(1).GetChild(7).GetChild(0).GetChild(0).gameObject;

instead of doing it as a foreach statement . But still i'm not satisfied because this is a bad practice . I don't know also how to shorten or optimize this code as a for loop statement.

EDIT: So from there i came up with this

for(int i=0; i<29; i++)
{
    Tests[i]= this. transform.GetChild(0).GetChild(1).GetChild(i+7).GetChild(0).GetChild(0).gameObject; 
}

1 Comment

thats not very generic either, a lot of magical numbers. i also doubt its less work than assigning the references in the inspector. just saying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.