Ways to activate and deactivate components on a gameobject?

How do I activate and deactivate or atleast destroy the diffrent components in unity scripting API

Like a FixedJoint I had to work around this by using this
FixedJoint.breakForce=0

instead of the old
FixedJoint.active=false

Please note I wasn’t using unity in the day’s of 2.0 So I may be dead wrong

Try setting the enabled property to false. if its a rigidbody, instead set the isKinematic to true and useGravity to false

To destroy a component, use the Destroy function and supply the component itself. Use GetComponent to get the component in script.

1 Like

Could you possiblly give me an example?

Start with:
gameObject.addComponent(FixedJoint)

Now show me how to destroy that component since I have heard you’re answer from the compiler but am still stumped :frowning:

I’m not sure but:

In C#:
Destroy(GetComponent());

In Unityscript:
Destroy(GetComponent(FixedJoint));

or to disable the component:

In C#:
FixedJoint f = GetComponent();
f.enabled = false;

In Unityscript:
var f : FixedJoint = GetComponent(FixedJoint)
f.enabled = false;

2 Likes

anomalous: Spot on, but with a small Syntax error. In UnityScript it should be:

var f : Fixed Joint = GetComponent(FixedJoint)
f.enabled = false

[/nitpicking]

1 Like

whoops, haha, I haven’t coded in UnityScript in a long time, thanks for the correction

1 Like
   var f : [B]FixedJoint[/B] = GetComponent(FixedJoint)
   f.enabled = false

[/also nitpicking]