daniel llee daniel llee - 3 years ago 114
C# Question

How can I find all the specific child gameobjects by name and add them to a list?

using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;

public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;

rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
[Header("Global Rotation")]
[Space(5)]
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public List<GameObject> allObjects;


[Space(5)]
[Header("Rotation Mode")]
[LabeledBool("Global Rotation", "Individual Rotation")]
[SerializeField]
bool _rotationMode = true;

[Header("Individual Rotation")]
[Space(3)]
public SpinableObject[] individualObjects;


private void Start()
{
allObjects = new List<GameObject>();
foreach(Transform t in transform)
{

}
}

// Update is called once per frame
void Update()
{
if (_rotationMode == false)
{

foreach (var spinner in individualObjects)
spinner.RotateObject();
}
else
{
for (int i = 0; i < allObjects.Count; i++)
{
RotateAllObjects(allObjects[i].transform);
}
}
}

private void RotateAllObjects(Transform t)
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;

rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}


Inside the Start i want to add the child objects by name not tag "Propeller"
I have 4 child objects: "Propeller1" , "Propeller2" , "Propeller3" , "Propeller4"

I want to add this 4 objects to allObjects
So it will rotate only the 4 properllers of this object the script is attached to. Since i'm cloning the object to many more i don't want to use FindByTag.

Answer Source

If you want to add all the children of the GameObject which content the script:

private void Start()
    {
        allObjects = new List<GameObject>();
        foreach(Transform child in transform)
        {
            allObjects.Add(child.gameObject)
        }
    }

Other options in case you would like to add children from other gameObjects:

You can find GameObjects by name. And even children using the path. For example:

   aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");

In case your children has the names you gave as example, you could do something like:

String Prefix = Propeller;
String nameGO;

for(int i = 1; i < 4; i++)
{
   nameGO = Propeller + i;
   GameObject mGameObject = transform.Find("PathToChildren/nameGO");
  //Now rotate or do something with it
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download