- Home /
Disabling all scripts on child only?
I actually have two problems here. I've tried disabling the component but it says the component doesn't exist (these a re custom scripts). Would I be able to just disable all scripts on a CHILD ONLY. This is for a multiplayer game so keep that in mind.
Answer by clunk47 · Nov 14, 2013 at 12:02 AM
Not fully understanding the question. But here is an example in C#. In this example, I have a cube with 3 other cubes as children. This script will be attached to the parent cube. This will destroy all components on all children except for the Transform component itself. To omit certain components, simply repeat what I did with the Transform component here.
using UnityEngine;
using System.Collections;
public class KillComponentsInChildren : MonoBehaviour
{
void Awake()
{
foreach(Transform t in transform)
{
foreach(Component com in t.GetComponents<Component>())
{
if(com != t.GetComponent<Transform>())
Destroy (com);
}
}
}
}
Alright so I have one more question then. How would I do this for just one of the childs? Not all of them.
Just tag it and use an if(tag) statement:
using UnityEngine;
using System.Collections;
public class $$anonymous$$illComponentsInChild : $$anonymous$$onoBehaviour
{
void Awake()
{
foreach(Transform t in transform)
{
if(t.tag == "Child")
{
foreach(Component com in t.GetComponents<Component>())
{
if(com != t.GetComponent<Transform>())
Destroy (com);
}
}
}
}
}
Your answer