- Home /
Disabling contant force?
private var conForce : ConstantForce;
function Start ()
{
conForce = GetComponent(ConstantForce).enabled = false;
}
Not really sure how to disable the constant force component, if there is a concrete way.
Thanks.
Answer by robertbu · Oct 17, 2013 at 07:50 PM
It is the double equals that is messing you up. Use:
GetComponent(ConstantForce).enabled = false;
Note that there is short cut variable as part of GameObject you can use instead:
constantForce.enabled = false;
Unity does a GetComponent() under the hood, so it doesn't save the call, but it makes code a bit cleaner.
Just so you know, turning off constant force will not stop your object from moving unless you have the drag turned way up.
Thanks for the information, sorted out my error! I'm getting a: NullReferenceException: Object reference not set to an instance of an object
When I run the game, after changing to said code. Do I need something else?
Cheers.
This code will only work if you have a ConstantForce component attached to the specific game object this script is attached to. Perhaps you misinterpret the meaning of ConstantForce?
The ConstantForce is added to my FPSController, of which my script also resides. $$anonymous$$aybe I do. Am I missing the point?
FPS controllers are typically built on character controllers, so they don't have Rigidbodies and therefore would not have a ConstantForce component. The FPS Input Controller pulls in a character motor and a Character Controller, but not a Rigidbody.
So I'm puzzled about what you are talking about. If you click on the game object this script is attached to, so you see a 'ConstantForce' script attached to that specific game object?
Yes. I didn't really explain myself thoroughly in the beginning post as I didn't think we'd have an issue. As you say the standard FPS has its prefabed components. I then added the constantforce which then comes tied with its own rigidbody. So essentially almost two physics components on one. This might not be a good way to do things. But was the only work around I could get for the issue I wanted to overcome. Hopefully that makes more sense.
Answer by clunk47 · Oct 17, 2013 at 09:56 PM
#pragma strict
var cForce : ConstantForce;
function Start()
{
if(GetComponent(ConstantForce))
cForce = GetComponent(ConstantForce);
if(cForce != null)
cForce.enabled = false;
}
Your answer
Follow this Question
Related Questions
Changing constant force component values via script? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Constant force question 1 Answer
Basic Enemy Javascript 0 Answers