Hi all,
So I have written a script that on mouse click does the following:
- Get the name of the object that was clicked on
- Search the objects children for a child with tag “Controllable”
- If it finds tag “Controllable” then add a Control Script to the child
- If an object with no “Controllable” child is clicked then remove active control scripts
So now I have a Cannon that I want to control using WASD. I click the cannon and bingo I can control its turret as its turret (child object) has the tag “Controllable”. I click off and bingo i can no longer control it. So it works.
Now I make another Cannon from the prefab. However now the Script only works between the 2 Cannons if the children in the Cannons have different names. For example I have 2 cannons, with 1 turret each, both turrets are named “turret”, script doesnt work as no matter which Cannon you click, it adds the script to the newest cannon. However if I rename “turret” on the new Cannon to “turret1”, bingo it works and I can click between cannons to control them.
Does anyone know why this is? Or how I can get around it?
My method for selecting the child looks like this:
private GameObject FindControllableObject(GameObject selectedObject)
{
Transform[] children = selectedObject.GetComponentsInChildren<Transform>(); //find all children and put them in array
foreach (Transform child in children)
{
if(child.tag == "Controllable")
{
GameObject controlChild = GameObject.Find(child.name);
return controlChild;
}
}
return null;
}