Enable Physics2d Component,enable component

Hi all!

I need to enable a component which is on my GameObject. I’m using the HingeJoint2d. Unfortunately I get an error that I can’t use the .enabled for this.

public class OpenDoor : MonoBehaviour {

float distBet;

public Transform doorControl;
public Transform bee;

private HingeJoint2D hj2d;
private Rigidbody2D rb2d;

void Start () {
	rb2d = GetComponent<Rigidbody2D> ();
	hj2d = GetComponent<HingeJoint2D> ();
}

void Update () {

	distBet = Vector2.Distance(doorControl.position, bee.position);
		Debug.Log(distBet);

	if(distBet <= 1)
	{
		hj2d.enabled;
	}

}

}

Quite new to all this. All your help would be super appreciated

Hello there,

And welcome to Unity Answers!

“enabled” isn’t a function you can call, it is actually a flag. That means, to switch it on or off, you need to call either hj2d.enabled = true; or hj2d.enabled = false;. Simply writing hj2d.enabled will tell you if the component is currently active or not. For example, Debug.Log(hj2d.enabled); would print “True” or “False” depending on the current state of the component.

For more info on this, I’d recommend reading this section of the manual. It will tell you all the things you need to know to understand components in Unity and how to use them.


Hope that helps!

Cheers,

~LegendBacon