- Home /
rigidbody constraints no longer part of unityscript or something?
Hey, Im using javascript or unityscript (whatever) and heres my code which, according to the documentation should stop movement of the rigidbody on the Z axis;
rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
Now, when I put this into my script to didn't work, at all, those names (constraints, etc...) are recognised by the editor or something, have the names changed or something?
Im using unity 3.0.0f5 pro...
I'm also not able to get this bit of code to work and can't seem to find any answers online. When I run the script, I get a NullReferenceException, and even though I added a conditional testing for a NRE:
if(rigidbody != null){
rigidbody.constraints = RigidbodyConstraints.FreezeNone;
} else {
Debug.Log("rigidbody is null for some mysterious reason");
}
I don't get my Debug message at all--just the same NR$$anonymous$$
Here's a simple script I made just for testing, which is attached to a basic object with a constraint of Z position (set in the Inspector).
function Start () { rigidbody.constraints = RigidbodyConstraints.FreezeNone; }
function Update () { rigidbody.AddForce(Vector3(0, 0, -5)); }
Same problem--NRE and the object doesn't move at all.
Any help would be greatly appreciated. Thanks.
Answer by save · Oct 21, 2011 at 10:15 AM
To clear things up a bit RigidbodyConstraints is an enumeration while constraints derived from Rigidbody is a variable. The constraints variable can take several layered values from the enumeration RigidbodyConstraints.
Doing this should freeze all motion on the Z-axis:
rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
To check if an object has a component named rigidbody use:
if (rigidbody)
If the script isn't on the object use:
if (theReference.rigidbody)
which for instance on a trigger could be:
function OnTriggerEnter (other : Collider) {
if (other.rigidbody)
}
Thanks for the reply. $$anonymous$$y problem was a misunderstanding of how rigidbody.constraints works and I was able to get it to work correctly by putting it into Update() rather than Start().
So for anybody having similar issues, to remove a Z axis position constraint on an object, the script should be:
function Update () {
rigidbody.constraints = RigidbodyConstraints.None;
rigidbody.AddForce(Vector3(0, 0, -5));
}
note: It is RigidbodyConstraints.None, not RigidbodyConstraints.FreezeNone, as this page suggests. The link on said page needs to be corrected.
Answer by PaulTurowski · Oct 21, 2011 at 09:19 AM
I'm also not able to get this bit of code to work and can't seem to find any answers online. When I run the script, I get a NullReferenceException, and even though I added a conditional testing for a NRE:
if(rigidbody != null){
rigidbody.constraints = RigidbodyConstraints.FreezeNone;
} else {
Debug.Log("rigidbody is null for some mysterious reason");
}
I don't get my Debug message at all--just the same NRE.
Any help would be greatly appreciated. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124842im_/https://answers.unity.com/themes/thub/images/avi.jpg)