Home > Blockchain >  How to check if x is a child of any object in array y
How to check if x is a child of any object in array y

Time:04-12

I'm creating a system where an effect is placed on every object in a set radius, however I have found that the effect is being placed on both the child of an object and its parent (eg. enemy and enemyHead). The problem comes when i realised there is a chance of the child being in the radius but the parent not, so i can't just delete the children from the array. How would i check if a child's parent is in the array so i can not apply the effect to it? (or any other way of going about the problem)

I'm aware I will need to use isChildOf, but i have a very limited understanding of it and how to apply it. I can't think of any way around this problem and really need to fixed it, I've made very little progress on the issue so far and haven't tried much to fix it.

Any help would be greatly appreciated, thanks.

CodePudding user response:

If you want to apply the effect to the parent of the child in radius then child.transform.parent will give you the parent of the child. If you want to check if x is child in array y then get the parent of child(x) and check if the parent exist in the array y. https://docs.unity3d.com/ScriptReference/Transform-parent.html

CodePudding user response:

There's a couple ways you could accomplish this:

First option: If you only wanted this effect placed on enemies you could check the tag, and only apply the effect to objects with the enemy tag:

if (objectToCheck.CompareTag("Enemy"))
{
    // code to apply
}

Other Option: Check if the object has a parent or not

Transform parent = objectToCheck.transform.parent;
if (parent == null)
{
    // Do your effect code
}
  • Related