- Home /
Problems enabling and disabling a script
Hello,
I have a small script that checks a variable, and if that variable is true, it will enable Rigidbody on the same GameObject and if it's false, it will disable it. I am remotely modifying the variable with another script but that's not the matter.
My problem is that .enabled is not recognized by Unity, and I get this error: Type UnityEngine.Rigidbody' does not contain a definition for
enabled' and no extension method enabled' of type
UnityEngine.Rigidbody' could be found (are you missing a using directive or an assembly reference?)
Here is the script:
public class ObjectSettings : MonoBehaviour {
public bool hasGravity;
public Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
}
void Update () {
if (hasGravity) {
rig.enabled = true;
}else {
rig.enabled = false;
}
}
}
Thanks!
Answer by tanoshimi · Apr 14, 2015 at 06:41 PM
You can't enable/disable a Rigidbody at runtime, and it has no enabled
property - which is exactly what the error message is telling you.
You probably either want to set isKinematic
to true, or else set detectCollisions
to false: http://docs.unity3d.com/ScriptReference/Rigidbody.html