Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SpeedTutor · Oct 17, 2013 at 07:31 PM · javascriptconstantforce

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SpeedTutor · Oct 17, 2013 at 08:03 PM 0
Share

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.

avatar image robertbu · Oct 17, 2013 at 08:05 PM 0
Share

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?

avatar image SpeedTutor · Oct 17, 2013 at 08:09 PM 0
Share

The ConstantForce is added to my FPSController, of which my script also resides. $$anonymous$$aybe I do. Am I missing the point?

avatar image robertbu · Oct 17, 2013 at 10:00 PM 0
Share

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?

avatar image SpeedTutor · Oct 17, 2013 at 10:11 PM 0
Share

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.

Show more comments
avatar image
2

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges