Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Disable a script in all Children

Discussion in 'Scripting' started by InMindStudios, Jan 27, 2021.

  1. InMindStudios

    InMindStudios

    Joined:
    Dec 5, 2020
    Posts:
    9
    Hey guys, brain is boiling over and as usual I have spent hours searching for a solution amongst outdated stuff...

    I have an empty object called Piedestaler.
    In this object i have twelve children called Piedstal(1)-(12).
    They each have a script called Splitable on them.

    When my sword hits one of the children(Piedestal) i want to deactivate the splitable script on all the children of Piedestaler
    .
    It feels like it should be so easy. something like
    OnTriggerEnter()
    find all the children, find the component script, turn script off.

    But for the life of me I cannot figure it out.
    Tried a gazillion different guides but nothing works.
    Help?
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    To receive a call to OnTriggerEnter, your object is going to need to have a rigidbody, and some colliders, or the thing that is hitting it will need a rigidbody, and also colliders.

    The best solution will depend on the exact makeup of this object and its children, as well as the makeup of the sword and the behavior of all of them together. But it sounds like you want your rigidbody on the empty parent object, and each of your children to have colliders alongside the splitable script, set to be triggers. Then, a script on the parent object, the one with the rigidbody, can have an OnTriggerEnter method that disables its children.
     
  3. InMindStudios

    InMindStudios

    Joined:
    Dec 5, 2020
    Posts:
    9

    I have no idea what you said but I appreciate your time.
    Everything with colliders and rigidbody and scripts works in my game. I only need a way to turn the script called splitable off in all children because when I split an object with my sword I want to stop being able to split others which will break the game if done to much.
     
  4. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Why not have a ChildComponentDisabler script on the parent that disables the relevant component in the children. Call it when a child receives a collision. Something like:

    On Piedestaler, the parent;
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ChildComponentDisabler : MonoBehaviour
    4. {
    5.     public void DisableSplitablesInChildren()
    6.     {
    7.         foreach (Transform child in transform)
    8.         {
    9.             Splitable splitable = child.GetComponent<Splitable>();
    10.  
    11.             if (splitable != null)
    12.             {
    13.                 splitable.enabled = false;
    14.             }
    15.         }
    16.     }
    17. }
    When one of your Piedstal children detect a collision, call something like this in its collision detection function:
    Code (CSharp):
    1. transform.GetComponentInParent<ChildComponentDisabler>().DisableSplitablesInChildren();
    There are improvements that could be made, you could cache all your children in the Start() function of ChildComponentDisabler so you haven't got to foreach through them each time. Can't say for sure if you want this though without knowing how often the children change. This should get you started though.

    Also I can relate to the fuzzy brain after too much thinking :) Don't burn out!
     
    Last edited: Jan 27, 2021