- Home /
Is script a child of another certain script?
So I'm working on a local multiplayer PvP kind of game and when one of the players dies I want to remove all the components that allow the player to interact with that now dead character.
Here is my Die() function:
public void Die() {
ArrayList toRemove = new ArrayList();
foreach (var component in gameObject.GetComponents<Component>()) {
if (component.GetType() == typeof(JAi) || component.GetType() == typeof(JAbility) || component.GetType() == typeof(JAim) || component.GetType() == typeof(JMovement) || component.GetType() == typeof(JController) || component.GetType() == typeof(JCollider) || component.GetType() == typeof(BoxCollider)) {
toRemove.Add(component);
}
}
foreach (var component in toRemove) {
Destroy((Component)component);
}
}
}
It removes all components from the object that allow for the player to control the character. Now the problem is that, for example "component.GetType() == typeof(JAbility)" will return false always since I just use JAbility as a parent class of all abilities, so something like "JAbilityStab" won't get removed since it isn't exactly of type JAbility, but rather, a child.
So is there any way to check "if script A is or is a child of script B"?
Answer by JoshuaMarkk · Oct 25, 2015 at 06:14 PM
Figured it out. The "is" operator does this as I want it to.
if (JAbilityStab is JAbility)
will return true!