Hello, quick question!
Sorry if that’s too basic, I looked into the documentation and I couldn’t find a function or method that returns the Components in a gameobject without the parent gameobject.
Thank you for your answers.
Hello, quick question!
Sorry if that’s too basic, I looked into the documentation and I couldn’t find a function or method that returns the Components in a gameobject without the parent gameobject.
Thank you for your answers.
You can do this:
var comps : yourType[];
comps : GetComponentsInChildren.<yourType>();
for(var comp : yourType in comps)
{
// Is my ID (the parent) not the same as the component's gameObject
if ( comp.gameObject.GetInstanceID() != GetInstanceID() )
{
}
}
Or in C#:
YourType[] comps = GetComponentsInChildren<yourType>();
foreach (YourType comp in comps)
{
if ( comp.gameObject.GetInstanceID() != GetInstanceID() )
{
}
}
I'm just wondering, is GetInstanceID faster than just comparing the objects? For exemple, would: if ( comp.gameObject != this.gameObject ) {...} be the same?
– Xnerdz@Xnerdz I was curious about this so I checked from the decompiled source, you can see <a href="https://github.com/MattRix/UnityDecompiled/blob/cc432a3de42b53920d5d5dae85968ff993f4ec0e/UnityEngine/UnityEngine/Object.cs#L100">here</a>: Comparing two UnityEngine.Objects essentially boils down to comparing their Instance IDs with a couple of extra checks. Maybe a little slower but probably not worth worrying about.
– Sarkahn@Sarkahn You're probably right. Anyway, by the time I wrote that comment, I learned that premature optimization is the root of all evil.
– XnerdzHere is a script that allows you to get all child script except the root object which I assume is what you meant by parent object (the very top one)
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //Necessary!!!!
public class NPCScript : MonoBehaviour {
void Start(){
HealthScript [] sc = GetCompNoRoot<HealthScript>(gameObject);
foreach(HealthScript s in sc){
s.health = 10;
}
}
T[] GetCompNoRoot<T>(GameObject obj)where T:Component{
List<T> tList = new List<T>();
foreach (Transform child in obj.transform.root)
{
T[] scripts = child.GetComponentsInChildren<T>();
if(scripts != null) {
foreach(T sc in scripts)
tList.Add (sc);
}
}
return tList.ToArray();
}
}
The method is GetCompNoRoot. It takes the script name as generic parameter and I added a game object parameter in case you wold store that method somewhere else like a Utility file.
All it does is create a list, get the root object of the parameter object and starts going down the hierarchy. In the end it returns an array of all scripts found.
Now if you meant no parent object as in no parent object simply gets this
foreach (Transform child in obj.transform.root)
to
foreach (Transform child in obj.transform)
Then if the script is on a child object in a hierarchy, it will ignore anything above but will go down also ignoring the object to which it is attached.
Note that this will work with any script or Component because of the constraint (where T: Component).
Thank you for your answer! I'm pretty sure your answer is correct, but Guy's way is more practical to my problem.
– Ranger-Ori
This works too (sorry question closed so put it here) foreach (Transform tr in transform) Debug.Log(tr.name); (from: http://forum.unity3d.com/threads/getcomponentsinchildren-not-parent-and-children.222009/#post-1480399)
– jeromeWork