deactivate/activate child/separate prefab from script attached to other object

with this javascript event that allows you to enable or disable the prefab it is attached to

gameObject.SetActive (false);

is there a way to set it so it can disable a prefab assigned to a transform var like this

var object : Transform;

function Start()
// edit this to disable the transform variable
gameObject.SetActive (false);


Hello there,

Absolutely there is. In this context, “gameObject” refers to the object your script is currently on. Meaning, it can only disable itself. If you want it to disable other objects, then you usually would need a reference to them.

Now SetActive() can only be called on GameObjects, so if you already have the assigned transform you would only need to get it’s GameObject component. To do that, you can simply do myTransformVariable.gameObject.SetActive(false), or object.gameObject.SetActive(false) in your case.

I hope that helps!




Sounds like you want to disable the component of an object that is parented. Meaning you want to disable the child of a gameObject. Its pretty easy actually but I am not certain of your situation so if I am wrong maybe you can clarify for me. So let’s pretend you have a camera parented to your player. Then all you need to do is this:

var cam: Camera;

function Start() {
    cam = GetComponentInChildren(Camera);

function Update() {
    cam.enabled = false;
    cam.enabled = true;