GetComponentInChildren with tag in two variable

Hello, for my problem I obviously look on the internet before coming here you ask the question. But when I use GetComponentInChildren <> it gives me the component of the first child but I want all the composent of the children.

I want take the component and place it in two variable named left and right.I want two variables with different children to control my side shooting with boolean:
Code to take component ShootCanonFront:

    ShootCanonfront left;
    ShootCanonfront right;

	void Start () {
        left = GameObject.FindWithTag("LeftBoat1").GetComponentInChildren<ShootCanonfront>();
        right = GameObject.FindWithTag("RightBoat1").GetComponentInChildren<ShootCanonfront>();
    }
	
	void Update () {
		
	}

119374-sans-titre.png

So my question is: How can i take ShootCanonFront components of childs Left object and place it to left variable and do the same for right variable.
I hope it’s understandable and thanks in advance ^^

Good day.

For one side you have GetComponentsInChildren () , ComponentS, with S

This gives you an array of that type of components. for example, a script called “GoodScript”

GoodScript[] MyScripts= [.bla bla...].GetComponentsInChildren<GoodScript>();

Now you have an array of all GoodScript s in the array called MyScripts.


By the other side, i recommend you , as there are only 2 components to find, to reach each object 1 by 1 with

gameObject.transform.Find("Child Name").GetComponent();

and get directly the components.

Byee!ª

:smiley:

Use

ShootCanonfront[] left;
ShootCanonfront[] right;

 void Start () {
     left = GameObject.FindWithTag("LeftBoat1").GetComponentsInChildren<ShootCanonfront>();
     right = GameObject.FindWithTag("RightBoat1").GetComponentsInChildren<ShootCanonfront>();
 }

Hi,

Try and use the following code.

{

List leftList = new List();

List rightList = new List();

ShootCanonfront left;

ShootCanonfront right;

void Start()

{

Transform leftBoat1 = GameObject.FindWithTag("LeftBoat1").GetComponentInChildren<ShootCanonfront>();

Transform rightBoat1 = GameObject.FindWithTag("RightBoat1").GetComponentInChildren<ShootCanonfront>();

foreach(Transform child in leftBoat1)

{

	leftList.Add(child);

}

foreach(Transform child in rightBoat1)

{

	rightList.Add(child);

}

left = leftList.ToArray();

right = rightList.ToArray();

}

}