- Home /
The question is answered, right answer was accepted
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 () {
}
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 ^^
Answer by tormentoarmagedoom · Jun 22, 2018 at 05:27 PM
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!ª
:D
Answer by Vicarian · Jun 22, 2018 at 05:28 PM
Use
ShootCanonfront[] left;
ShootCanonfront[] right;
void Start () {
left = GameObject.FindWithTag("LeftBoat1").GetComponentsInChildren<ShootCanonfront>();
right = GameObject.FindWithTag("RightBoat1").GetComponentsInChildren<ShootCanonfront>();
}
Hello, when i try this i have an error and i can access to the variable of ShootcanonFront. Thank to you anyway :) This error:`error CS1061: Type ShootCanonFront[]' does not contain a definition for
isNowShooting' and no extension method isNowShooting' of type
ShootCanonFront[]' could be found. Are you missing an assembly reference? `
It's a collection, so you have to step through it in a loop. Use a for or foreach, whichever you're comfortable with:
for (int i = 0; i < left.Length; ++i)
{
// Prints the name of the object containing the component
Debug.Log(left[i].name);
}
or
foreach (ShootCanonfront scf in left)
{
// Prints the name of the object containing the component
Debug.Log(scf.name);
}
Do also make use of the scripting reference: https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
Sorry but it's the same. I have the same error, i can't access to my variable isNowShooting. $$anonymous$$y whole code:`
ShootCanonFront[] left; ShootCanonFront[] right;
void Start () { left = GameObject.FindWithTag("LeftBoat1").GetComponentsInChildren(); right = GameObject.FindWithTag("RightBoat1").GetComponentsInChildren(); foreach (ShootCanonFront scf in left) { Debug.Log(scf.name); } rgBoat = GetComponent(); health = startHealth; }
void Side()
{
//Change side of boat
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
SelectedArm += 1;
if (SelectedArm == SelectedArmLeft)
{
left.isNowShooting = true; right.isNowShooting = false;
}
if (SelectedArm == SelectedArmRight)
{
left.isNowShooting = false; right.isNowShooting = true;
}
if (SelectedArm > SelectedArmRight)
{
SelectedArm = SelectedArmLeft; left.isNowShooting = true; right.isNowShooting = false;
}
}
}
Error:error CS1061: Type `ShootCanonFront[]' does not contain a definition for `isNowShooting' and no extension method `isNowShooting' of type `ShootCanonFront[]' could be found. Are you missing an assembly reference?`
Answer by Zgull · Jun 22, 2018 at 05:39 PM
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();
Transform rightBoat1 = GameObject.FindWithTag("RightBoat1").GetComponentInChildren();
foreach(Transform child in leftBoat1)<br />
{<br />
leftList.Add(child);<br />
}<br /><br />
foreach(Transform child in rightBoat1)<br />
{<br />
rightList.Add(child);<br />
}<br /><br />
left = leftList.ToArray();<br />
right = rightList.ToArray();<br />
}
}
Follow this Question
Related Questions
Make a simple tree 1 Answer
How do i find the closest child? 1 Answer
"Center On Children" programmatically 1 Answer
Check if there is a child of object, and if so, get tag? 2 Answers
Can one specify the parent of a gameobject in an array? 2 Answers